自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

關于讀寫鎖算法的Java實現(xiàn)及思考

開發(fā) 后端 算法
問題背景:多個線程對一個共享的資源進行讀寫訪問。寫線程之間需要互斥,讀線程跟寫線程需要互斥,讀線程之間不用互斥。

問題背景:多個線程對一個共享的資源進行讀寫訪問。寫線程之間需要互斥,讀線程跟寫線程需要互斥,讀線程之間不用互斥。

早些時候聽張sir的課,講述java5中增強并發(fā)的功能。用java.util.concurrent.locks中ReadWriteLock 可以輕松解決讀寫鎖問題。我在思考如果沒有ReadWriteLock,單靠synchronized可以怎樣做呢? 的確,比較麻煩。

1.結合張sir傳授的面向對象的設計思想,首先設計一個業(yè)務類Business作為共享資源,封裝write跟read方法。

2.因為write必定互斥,所以直接定義synchronized。

3.read之間不要互斥 所以read 不能直接定義synchronized的 但是 write跟read 需要互斥 如何控制 我想到的一個方法是在read里 加入synchronized(this){} 同時定義readThreads計數(shù)器作為信號量 我試想下會出現(xiàn)下面幾種情況:

read[m]表示某個線程的read方法 。

write[n] 同上

1>read[m]中執(zhí)行到synchronized(this){readThreads++;}時 write[n]來了 write[n] 會被自身的synchronized阻塞。

2>read[m]在do something(此時無鎖)時 write[n] 來了 因為 readThreads!=0 而被迫wait。

3> 每次read[m]結束時 wait中的write[n]會被notify 但如果發(fā)現(xiàn)還有其他的read的話 write[n] 只能無奈地再次wait。

4>當readThreads==0并且調用notifyAll 時 read[m] 和 write[n] 會競爭cpu 如果write[n]再次落敗,則會出現(xiàn)1>或3> ; 如果成了,則如下:

5> 如果write[n] wait中醒來占鎖,read[m]被阻塞synchronized(this){readThreads++;}之上。

6>如果被阻塞的write[n]占鎖,read[m]被阻塞synchronized(this){readThreads++;}之上。

從以上看來read 和 write 是互斥的。

4.實現(xiàn)細節(jié)如下:<如有錯誤歡迎指出交流>

  1. package communication;  
  2. import java.util.Random;  
  3.  
  4. public class ReadWriteLockTest {  
  5.         public static void main(String[] args){  
  6.                 final Business business = new Business();  
  7.                   
  8.                 //啟動4線程 2讀 2寫  
  9.                 for(int i=1;i<=2;i++){  
  10.                                 new Thread(new Runnable(){  
  11.                                         public void run() {  
  12.                                                 for(int j=1;j<1000;j++){  
  13.                                                         business.read();  
  14.                                                         try {  
  15.                                                                 Thread.sleep(900);  
  16.                                                         } catch (InterruptedException e) {  
  17.                                                                 e.printStackTrace();  
  18.                                                         }                                                          
  19.                                                 }                                          
  20.                                         }                                  
  21.                                 }).start();  
  22.                                   
  23.                                 new Thread(new Runnable(){  
  24.                                         public void run() {  
  25.                                                 Random r = new Random();  
  26.                                                 for(int j=1;j<1000;j++){  
  27.                                                         int i = r.nextInt(100);  
  28.                                                         business.write(i);  
  29.                                                         try {  
  30.                                                                 Thread.sleep(1000);  
  31.                                                         } catch (InterruptedException e) {  
  32.                                                                 e.printStackTrace();  
  33.                                                         }  
  34.                                                 }                                          
  35.                                         }                  
  36.                                 }).start();  
  37.                 }  
  38.                   
  39.         }  
  40.           
  41. }  
  42. //封裝的業(yè)務類  
  43. class Business{  
  44.         private int data=0//共享資源屬性  
  45.         private int readThreads = 0//讀線程數(shù)  
  46.         //private boolean isWriting  = false;   
  47.         //是否執(zhí)行寫 后來發(fā)現(xiàn)不需要 當write搶占鎖時 所有的read 都被擋在synchronized (this){}之上 無機會執(zhí)行wait  
  48.         public void read(){  
  49.                 synchronized (this) {          
  50.                         /*while(isWriting){  
  51.                                 try {                          
  52.                                         this.wait();                                          
  53.                                 } catch (InterruptedException e) {  
  54.                                         // TODO Auto-generated catch block  
  55.                                         e.printStackTrace();  
  56.                                 }  
  57.                         }*/ 
  58.                         //readThreads不被鎖的話 會出現(xiàn)read和write不互斥的小概率事件 導致線程不安全  
  59.                         readThreads++;  
  60.                           
  61.                 }  
  62.                   
  63.                 System.out.println(Thread.currentThread().getName()+" read begin");  
  64.                 System.out.println(Thread.currentThread().getName()+" read:"+data);  
  65.                 System.out.println(Thread.currentThread().getName()+" read finish");  
  66.                           
  67.                 synchronized (this) {  
  68.                         readThreads--;  
  69.                         this.notifyAll();  
  70.                 }          
  71.         }  
  72.           
  73.         public synchronized void write(int i){  
  74.                 while(readThreads != 0){//當read 正處于do something狀態(tài)時 來個write 那就只有等等先了  
  75.                         try {  
  76.                                 this.wait();  
  77.                         } catch (InterruptedException e) {  
  78.                                 e.printStackTrace();  
  79.                         }  
  80.                 }  
  81.                 //isWriting = true;  
  82.                 System.out.println(Thread.currentThread().getName()+" write start");  
  83.                 data = i;  
  84.                 System.out.println(Thread.currentThread().getName()+" write:"+i);  
  85.                 System.out.println(Thread.currentThread().getName()+" write over");  
  86.                 //isWriting = false;  
  87.                 this.notifyAll();  
  88.         }  

思考中:

5.當讀頻繁時 readThreads會長時間!= 0 寫線程會餓死 這個可以如何解決?

原文鏈接:http://www.cnblogs.com/hottea4Goodspeed/archive/2012/03/06/2381257.html

【編輯推薦】

  1. 6個提高Java開發(fā)者效率的工具
  2. Java并發(fā):juc Executor框架詳解
  3. 設計Java應用程序的平滑停止
  4. Spock 0.6發(fā)布 Java測試框架
  5. 深入Java探索:Java內存區(qū)域
責任編輯:林師授 來源: Goodspeed85的博客
相關推薦

2024-01-29 01:08:01

悲觀鎖遞歸鎖讀寫鎖

2023-01-04 13:43:24

讀寫鎖AQS共享模式

2011-04-13 14:04:14

Java數(shù)組

2015-11-03 09:24:12

Java讀寫鎖分析

2020-09-16 10:47:26

數(shù)字化轉型企業(yè)領導者CIO

2024-12-27 10:51:53

2009-11-28 20:24:13

Linux互斥鎖同步移植

2019-11-28 16:00:06

重入鎖讀寫鎖樂觀鎖

2023-03-10 15:45:03

Golang公平鎖

2024-08-12 17:36:54

2011-08-30 09:59:47

Mysql ProxyLUA

2020-05-12 14:03:51

RedisZooKeeper分布式鎖

2011-06-03 13:03:03

JAVA

2021-12-08 10:54:09

汽車智能芯片

2022-08-01 07:38:29

代碼開發(fā)

2024-05-15 09:41:22

樂觀鎖編程

2024-05-08 10:20:00

Redis分布式

2010-06-07 15:12:12

Cacti配置

2024-10-10 09:40:29

2012-11-08 11:19:38

點贊
收藏

51CTO技術棧公眾號