程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> 去故就新 Java線程新同步機制(2)

去故就新 Java線程新同步機制(2)

編輯:關於JAVA

3、讀寫鎖ReentrantReadWriteLock,適用於"讀多寫少"的多線程應用場景,"讀-寫"互斥,"寫-寫"互斥,而讀-讀可以共享同讀鎖,即一個線程獲取讀鎖,其它線程可直接進入讀,不會被阻塞。

(1) 相關接口

創建讀寫鎖對象

  1. ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock();

獲取讀鎖

  1. Lock readLock = rwLock.readLock();

獲取寫鎖

  1. Lock writeLock = rwLock.writeLock();

(2).讀寫鎖使用基本結構

  1. //對所有的讀操作添加讀鎖
  2. readLock.lock();
  3. try{
  4. //code to read
  5. }finally{
  6. readLock.unlock();
  7. }
  1. //對所有的寫操作添加寫鎖
  2. writeLock.lock();
  3. try{
  4. //code to write
  5. }finally{
  6. writeLock.unlock();
  7. }

(3) 測試代碼:

  1. import Java.util.concurrent.locks.Lock;
  2. import Java.util.concurrent.locks.ReentrantReadWriteLock;
  3. class Reader extends Thread {
  4. private Lock readLock = null;
  5. public Reader(Lock readLock) {
  6. this.readLock = readLock;
  7. }
  8. public void run() {
  9. while (true) {
  10. readLock.lock();
  11. try {
  12. System.out.println(Thread.currentThread().getName()
  13. + ":read action for 1 seconds-"+ReadWriteLock.testVal);
  14. } finally {
  15. readLock.unlock();
  16. }
  17. try {
  18. Thread.sleep(1000);
  19. } catch (InterruptedException e) {
  20. e.printStackTrace();
  21. }
  22. }
  23. }
  24. }
  25. class Writer extends Thread {
  26. private Lock writeLock = null;
  27. public Writer(Lock writeLock) {
  28. this.writeLock = writeLock;
  29. }
  30. public void run() {
  31. while (true) {
  32. writeLock.lock();
  33. try {
  34. System.out.println(Thread.currentThread().getName()
  35. + ":write action for 2 seconds");
  36. if(ReadWriteLock.testVal.equals("1111"))
  37. ReadWriteLock.testVal = "2222";
  38. else
  39. ReadWriteLock.testVal = "1111";
  40. } finally {
  41. writeLock.unlock();
  42. }
  43. try {
  44. Thread.sleep(2000);
  45. } catch (InterruptedException e) {
  46. e.printStackTrace();
  47. }
  48. }
  49. }
  50. }
  51. public class ReadWriteLock {
  52. public static String testVal = "Initiation";
  53. public static void main(String[] args) {
  54. ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
  55. Lock readLock = lock.readLock();
  56. Lock writeLock = lock.writeLock();
  57. Reader reader1 = new Reader(readLock);
  58. reader1.setName("reader1");
  59. Reader reader2 = new Reader(readLock);
  60. reader2.setName("reader2");
  61. Reader reader3 = new Reader(readLock);
  62. reader3.setName("reader3");
  63. Reader reader4 = new Reader(readLock);
  64. reader4.setName("reader4");
  65. Writer writer = new Writer(writeLock);
  66. writer.setName("writer1");
  67. reader1.start();
  68. reader2.start();
  69. reader3.start();
  70. reader4.start();
  71. writer.start();
  72. }
  73. }

輸出:

reader1:read action for 1 seconds-Initiation
reader3:read action for 1 seconds-Initiation
writer1:write action for 2 seconds
reader2:read action for 1 seconds-1111
reader4:read action for 1 seconds-1111
reader3:read action for 1 seconds-1111
reader1:read action for 1 seconds-1111
reader4:read action for 1 seconds-1111
reader2:read action for 1 seconds-1111
writer1:write action for 2 seconds
reader4:read action for 1 seconds-2222
reader1:read action for 1 seconds-2222
reader3:read action for 1 seconds-2222
reader2:read action for 1 seconds-2222

4、總結

Lock接口替代synchronized

Lock接口可以比sychronized提供更廣泛的鎖定操作,可以提供多把不同的鎖,且鎖之間互不干涉。

Lock接口提供lock()與unlock()方法,使用明確調用來完成同步的,OO思想好於前者。

Lock可以自由操控同步范圍(scope)。

Lock接口支持nested lock(嵌套鎖定),並提供了豐富的api。

Lock接口提供了tryLock()方法,支持嘗試取得某個object lock。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved