ReadWriteLock

多线程读写同一个对象的数据是很普遍的,通常,要避免读写冲突,必须保证任何时候仅有一个线程在写入,有线程正在读取的时候,写入操作就必须等待。简单说,就是要避免“写-写”冲突和“读-写”冲突。但是同时读是允许的,因为“读-读”不冲突,而且很安全。


要实现以上的ReadWriteLock,简单的使用synchronized就不行,我们必须自己设计一个ReadWriteLock类,在读之前,必须先获得“读锁”,写之前,必须先获得“写锁”。举例说明:

DataHandler对象保存了一个可读写的char[]数组:



java 代码

1. package
2.   
3. public class
4. // store data:
5. private char[] buffer = "AAAAAAAAAA".toCharArray();  
6.   
7. private char[] doRead() {  
8. char[] ret = new char[buffer.length];  
9. for(int i=0; i  
10.             ret[i] = buffer[i];  
11. 3);  
12.         }  
13. return
14.     }  
15.   
16. private void doWrite(char[] data) {  
17. if(data!=null) {  
18. new char[data.length];  
19. for(int i=0; i  
20.                 buffer[i] = data[i];  
21. 10);  
22.             }  
23.         }  
24.     }  
25.   
26. private void sleep(int
27. try
28.             Thread.sleep(ms);  
29.         }  
30. catch(InterruptedException ie) {}  
31.     }  
32. }



doRead()和doWrite()方法是非线程安全的读写方法。为了演示,加入了sleep(),并设置读的速度大约是写的3倍,这符合通常的情况。


为了让多线程能安全读写,我们设计了一个ReadWriteLock:



java 代码

1. package
2. public class
3. private int readingThreads = 0;  
4. private int writingThreads = 0;  
5. private int waitingThreads = 0; // waiting for write
6. private boolean preferWrite = true;  
7.   
8. public synchronized void readLock() throws
9. while(writingThreads>0 || (preferWrite && waitingThreads>0))  
10. this.wait();  
11.         readingThreads++;  
12.     }  
13.   
14. public synchronized void
15.         readingThreads--;  
16. true;  
17.         notifyAll();  
18.     }  
19.   
20. public synchronized void writeLock() throws
21.         waitingThreads++;  
22. try
23. while(readingThreads>0 || writingThreads>0)  
24. this.wait();  
25.         }  
26. finally
27.             waitingThreads--;  
28.         }  
29.         writingThreads++;  
30.     }  
31.   
32. public synchronized void
33.         writingThreads--;  
34. false;  
35.         notifyAll();  
36.     }  
37. }


readLock()用于获得读锁,readUnlock()释放读锁,writeLock()和writeUnlock()一样。由于锁用完必须释放,因此,必须保证lock和unlock匹配。我们修改DataHandler,加入ReadWriteLock:




java 代码

1. package
2. public class
3. // store data:
4. private char[] buffer = "AAAAAAAAAA".toCharArray();  
5. // lock:
6. private ReadWriteLock lock = new
7.   
8. public char[] read(String name) throws
9. " waiting for read...");  
10.         lock.readLock();  
11. try
12. char[] data = doRead();  
13. " reads data: " + new
14. return
15.         }  
16. finally
17.             lock.readUnlock();  
18.         }  
19.     }  
20.   
21. public void write(String name, char[] data) throws
22. " waiting for write...");  
23.         lock.writeLock();  
24. try
25. " wrote data: " + new
26.             doWrite(data);  
27.         }  
28. finally
29.             lock.writeUnlock();  
30.         }  
31.     }  
32.   
33. private char[] doRead() {  
34. char[] ret = new char[buffer.length];  
35. for(int i=0; i  
36.             ret[i] = buffer[i];  
37. 3);  
38.         }  
39. return
40.     }  
41. private void doWrite(char[] data) {  
42. if(data!=null) {  
43. new char[data.length];  
44. for(int i=0; i  
45.                 buffer[i] = data[i];  
46. 10);  
47.             }  
48.         }  
49.     }  
50. private void sleep(int
51. try
52.             Thread.sleep(ms);  
53.         }  
54. catch(InterruptedException ie) {}  
55.     }  
56. }


public方法read()和write()完全封装了底层的ReadWriteLock,因此,多线程可以安全地调用这两个方法:



java 代码



 

1. // ReadingThread不断读取数据:
2. package
3. public class ReadingThread extends
4. private
5. public
6. this.handler = handler;  
7.     }  
8. public void
9. for(;;) {  
10. try
11. char[] data = handler.read(getName());  
12. long)(Math.random()*1000+100));  
13.             }  
14. catch(InterruptedException ie) {  
15. break;  
16.             }  
17.         }  
18.     }  
19. }  
20.   
21. // WritingThread不断写入数据,每次写入的都是10个相同的字符:
22. package
23. public class WritingThread extends
24. private
25. public
26. this.handler = handler;  
27.     }  
28. public void
29. char[] data = new char[10];  
30. for(;;) {  
31. try
32.                 fill(data);  
33.                 handler.write(getName(), data);  
34. long)(Math.random()*1000+100));  
35.             }  
36. catch(InterruptedException ie) {  
37. break;  
38.             }  
39.         }  
40.     }  
41. // 产生一个A-Z随机字符,填入char[10]:
42. private void fill(char[] data) {  
43. char c = (char)(Math.random()*26+'A');  
44. for(int i=0; i  
45.             data[i] = c;  
46.     }  
47. }

最后Main负责启动这些线程:



java 代码

1. package
2. public class
3. public static void
4. new
5. new
6. new
7. new
8. new
9. new
10. new
11. new
12. new
13.         };  
14. for(int i=0; i  
15.             ts[i].start();  
16.         }  
17.     }  
18. }



我们启动了5个读线程和2个写线程,运行结果如下:

Thread-0 waiting for read...
 Thread-1 waiting for read...
 Thread-2 waiting for read...
 Thread-3 waiting for read...
 Thread-4 waiting for read...
 Thread-5 waiting for write...
 Thread-6 waiting for write...
 Thread-4 reads data: AAAAAAAAAA
 Thread-3 reads data: AAAAAAAAAA
 Thread-2 reads data: AAAAAAAAAA
 Thread-1 reads data: AAAAAAAAAA
 Thread-0 reads data: AAAAAAAAAA
 Thread-5 wrote data: EEEEEEEEEE
 Thread-6 wrote data: MMMMMMMMMM
 Thread-1 waiting for read...
 Thread-4 waiting for read...
 Thread-1 reads data: MMMMMMMMMM
 Thread-4 reads data: MMMMMMMMMM
 Thread-2 waiting for read...
 Thread-2 reads data: MMMMMMMMMM
 Thread-0 waiting for read...
 Thread-0 reads data: MMMMMMMMMM
 Thread-4 waiting for read...
 Thread-4 reads data: MMMMMMMMMM
 Thread-2 waiting for read...
 Thread-5 waiting for write...
 Thread-2 reads data: MMMMMMMMMM
 Thread-5 wrote data: GGGGGGGGGG
 Thread-6 waiting for write...
 Thread-6 wrote data: AAAAAAAAAA
 Thread-3 waiting for read...
 Thread-3 reads data: AAAAAAAAAA
 ......

可以看到,每次读/写都是完整的原子操作,因为我们每次写入的都是10个相同字符。并且,每次读出的都是最近一次写入的内容。

如果去掉ReadWriteLock:


java 代码

1. package
2. public class
3.   
4. // store data:
5. private char[] buffer = "AAAAAAAAAA".toCharArray();  
6.   
7. public char[] read(String name) throws
8. char[] data = doRead();  
9. " reads data: " + new
10. return
11.     }  
12. public void write(String name, char[] data) throws
13. " wrote data: " + new
14.         doWrite(data);  
15.     }  
16.   
17. private char[] doRead() {  
18. char[] ret = new char[10];  
19. for(int i=0; i<10; i++) {  
20.             ret[i] = buffer[i];  
21. 3);  
22.         }  
23. return
24.     }  
25. private void doWrite(char[] data) {  
26. for(int i=0; i<10; i++) {  
27.             buffer[i] = data[i];  
28. 10);  
29.         }  
30.     }  
31. private void sleep(int
32. try
33.             Thread.sleep(ms);  
34.         }  
35. catch(InterruptedException ie) {}  
36.     }  
37. }



运行结果如下:


Thread-5 wrote data: AAAAAAAAAA
Thread-6 wrote data: MMMMMMMMMM
Thread-0 reads data: AAAAAAAAAA
Thread-1 reads data: AAAAAAAAAA
Thread-2 reads data: AAAAAAAAAA
Thread-3 reads data: AAAAAAAAAA
Thread-4 reads data: AAAAAAAAAA
Thread-2 reads data: MAAAAAAAAA
Thread-3 reads data: MAAAAAAAAA
Thread-5 wrote data: CCCCCCCCCC
Thread-1 reads data: MAAAAAAAAA
Thread-0 reads data: MAAAAAAAAA
Thread-4 reads data: MAAAAAAAAA
Thread-6 wrote data: EEEEEEEEEE
Thread-3 reads data: EEEEECCCCC
Thread-4 reads data: EEEEEEEEEC
Thread-1 reads data: EEEEEEEEEE

可以看到在Thread-6写入EEEEEEEEEE的过程中,3个线程读取的内容是不同的。

思考

java的synchronized提供了最底层的物理锁,要在synchronized的基础上,实现自己的逻辑锁,就必须仔细设计ReadWriteLock。

Q: lock.readLock()为什么不放入try{ } 内?
A: 因为readLock()会抛出InterruptedException,导致readingThreads++不执行,而readUnlock()在 finally{ } 中,导致readingThreads--执行,从而使readingThread状态出错。writeLock()也是类似的。

Q: preferWrite有用吗?
A: 如果去掉preferWrite,线程安全不受影响。但是,如果读取线程很多,上一个线程还没有读取完,下一个线程又开始读了,就导致写入线程长时间无法 获得writeLock;如果写入线程等待的很多,一个接一个写,也会导致读取线程长时间无法获得readLock。preferWrite的作用是让读 /写交替执行,避免由于读线程繁忙导致写无法进行和由于写线程繁忙导致读无法进行。

Q: notifyAll()换成notify()行不行?
A: 不可以。由于preferWrite的存在,如果一个线程刚读取完毕,此时preferWrite=true,再notify(),若恰好唤醒的是一个读 线程,则while(writingThreads>0 || (preferWrite && waitingThreads>0))可能为true导致该读线程继续等待,而等待写入的线程也处于wait()中,结果所有线程都处于wait ()状态,谁也无法唤醒谁。因此,notifyAll()比notify()要来得安全。程序验证notify()带来的死锁:

Thread-0 waiting for read...
Thread-1 waiting for read...
Thread-2 waiting for read...
Thread-3 waiting for read...
Thread-4 waiting for read...
Thread-5 waiting for write...
Thread-6 waiting for write...
Thread-0 reads data: AAAAAAAAAA
Thread-4 reads data: AAAAAAAAAA
Thread-3 reads data: AAAAAAAAAA
Thread-2 reads data: AAAAAAAAAA
Thread-1 reads data: AAAAAAAAAA
Thread-5 wrote data: CCCCCCCCCC
Thread-2 waiting for read...
Thread-1 waiting for read...
Thread-3 waiting for read...
Thread-0 waiting for read...
Thread-4 waiting for read...
Thread-6 wrote data: LLLLLLLLLL
Thread-5 waiting for write...
Thread-6 waiting for write...
Thread-2 reads data: LLLLLLLLLL
Thread-2 waiting for read...
(运行到此不动了)

注意到这种死锁是由于所有线程都在等待别的线程唤醒自己,结果都无法醒过来。这和两个线程希望获得对方已有的锁造成死锁不同。因此多线程设计的难度远远高于单线程应用。

从JDK 5开始,java.util.concurrent包就已经包含了ReadWriteLock,使用更简单,无需我们自行实现上述代码。但是,理解ReadWriteLock的原理仍非常重要。