Writing multithreaded java applications is not a piece of cake. Extra care must be taken because bad synchronization can bring your application to its knees. The JVM heap is shared by all the threads. If multiple threads need to use the same objects or static class variables concurrently, thread access to shared data must be carefuly managed. Since version 1.5, utility classes commonly useful in concurrent programming is included in the JSDK.
In Java
synchronized keyword is used to acquire a exclusive lock on an object. When a thread acquires a lock of an object either for reading or writing, other threads must wait until the lock on that object is released. Think of a scenerio that there are many reader threads that reads a shared data frequently and only one writer thread that updates shared data. It's not necessary to exclusively lock access to shared data while reading because multiple read operations can be done in parallel unless there is a write operation.
In this post i'll give an example usage of
ReadWriteLock interface which is introduced in the
Java 1.5 API Doc. In Java Api Documentation it says :
A ReadWriteLock maintains a pair of associated locks,
one for read-only operations and one for writing.
The read lock may be held simultaneously by multiple reader threads,
so long as there are no writers. The write lock is exclusive.
Reader threads can read shared data simultaneously. A read operation does not block other read operations. This is the case when you execute an SQL SELECT statement. But write operation is exclusive. This means all readers and other writers are blocked when a writer thread holds the lock for modifing shared data.
Writer.java This class represents a thread that updates shared data. Writer uses WriteLock of ReadWriteLock to exclusively lock access to dictionary.
01 package deneme.readwritelock;
02
03
04 public class Writer extends Thread{
05 private boolean runForestRun = true;
06 private Dictionary dictionary = null;
07
08 public Writer(Dictionary d, String threadName) {
09 this.dictionary = d;
10 this.setName(threadName);
11 }
12 @Override
13 public void run() {
14 while (this.runForestRun) {
15 String [] keys = dictionary.getKeys();
16 for (String key : keys) {
17 String newValue = getNewValueFromDatastore(key);
18 //updating dictionary with WRITE LOCK
19 dictionary.set(key, newValue);
20 }
21
22 //update every seconds
23 try {
24 Thread.sleep(1000);
25 } catch (InterruptedException e) {
26 e.printStackTrace();
27 }
28 }
29 }
30 public void stopWriter(){
31 this.runForestRun = false;
32 this.interrupt();
33 }
34 public String getNewValueFromDatastore(String key){
35 //This part is not implemented. Out of scope of this artile
36 return "newValue";
37 }
38 }
Reader.java This class represents a thread that reads share data.
01 package deneme.readwritelock;
02
03 public class Reader extends Thread{
04
05 private Dictionary dictionary = null;
06 public Reader(Dictionary d, String threadName) {
07 this.dictionary = d;
08 this.setName(threadName);
09 }
10
11 private boolean runForestRun = true;
12 @Override
13 public void run() {
14 while (runForestRun) {
15 String [] keys = dictionary.getKeys();
16 for (String key : keys) {
17 //reading from dictionary with READ LOCK
18 String value = dictionary.get(key);
19
20 //make what ever you want with the value.
21 System.out.println(key + " : " + value);
22 }
23
24 //update every seconds
25 try {
26 Thread.sleep(1000);
27 } catch (InterruptedException e) {
28 e.printStackTrace();
29 }
30 }
31 }
32
33 public void stopReader(){
34 this.runForestRun = false;
35 this.interrupt();
36 }
37 }
Dictionary.java This is a simple and thread safe dictionary. Read operations are managed through ReadLock and write operations (updates) are managed throuh WriteLock.
01 package deneme.readwritelock;
02
03 import java.util.HashMap;
04 import java.util.concurrent.locks.Lock;
05 import java.util.concurrent.locks.ReentrantReadWriteLock;
06
07 public class Dictionary {
08
09 private final ReentrantReadWriteLock readWriteLock =
10 new ReentrantReadWriteLock();
11
12 private final Lock read = readWriteLock.readLock();
13
14 private final Lock write = readWriteLock.writeLock();
15
16 private HashMap<String, String> dictionary = new HashMap<String, String>();
17
18 public void set(String key, String value) {
19 write.lock();
20 try {
21 dictionary.put(key, value);
22 } finally {
23 write.unlock();
24 }
25 }
26
27 public String get(String key) {
28 read.lock();
29 try{
30 return dictionary.get(key);
31 } finally {
32 read.unlock();
33 }
34 }
35
36 public String[] getKeys(){
37 read.lock();
38 try{
39 String keys[] = new String[dictionary.size()];
40 return dictionary.keySet().toArray(keys);
41 } finally {
42 read.unlock();
43 }
44 }
45
46 public static void main(String[] args) {
47 Dictionary dictionary = new Dictionary();
48 dictionary.set("java", "object oriented");
49 dictionary.set("linux", "rulez");
50 Writer writer = new Writer(dictionary, "Mr. Writer");
51 Reader reader1 = new Reader(dictionary ,"Mrs Reader 1");
52 Reader reader2 = new Reader(dictionary ,"Mrs Reader 2");
53 Reader reader3 = new Reader(dictionary ,"Mrs Reader 3");
54 Reader reader4 = new Reader(dictionary ,"Mrs Reader 4");
55 Reader reader5 = new Reader(dictionary ,"Mrs Reader 5");
56 writer.start();
57 reader1.start();
58 reader2.start();
59 reader3.start();
60 reader4.start();
61 reader5.start();
62 }
63
64 }