一篇关于ReadWriteLock不错的文章

  1. package concurrent;  
  2.   
  3. import java.util.Random;  
  4. import java.util.concurrent.locks.ReentrantReadWriteLock;  
  5. import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock;  
  6. import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock;  
  7.   
  8. /** 
  9.  *1、readLock可重入; 
  10.  *2、writeLock必须等待所有的readLock释放后才可进入; 
  11.  *3、writeLock范围内,readLock不可进入 
  12.  */  
  13. public class ReadWriteLockTest {  
  14.   
  15.     private ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();  
  16.   
  17.     private ReadLock readLock = readWriteLock.readLock();  
  18.   
  19.     private WriteLock writeLock = readWriteLock.writeLock();  
  20.   
  21.     private String shareData = "寂寞等待中。。。";  
  22.   
  23.     public ReadWriteLockTest() {  
  24.     }  
  25.   
  26.     public void write(String str) {       
  27.         writeLock.lock();//可用tryLock()干点其他事  
  28. //      if(!writeLock.tryLock()){  
  29. //          System.err.println("读锁数量:"+readWriteLock.getReadLockCount());  
  30. //          return;  
  31. //      }  
  32.         System.err.println("ThreadName:" + Thread.currentThread().getName() + "    locking...");  
  33.         try {  
  34.             shareData = str;  
  35.             System.err.println("ThreadName:" + Thread.currentThread().getName() + "   修改为:" + str);  
  36.             Thread.sleep(50);  
  37.         } catch (InterruptedException e) {  
  38.             e.printStackTrace();  
  39.         } finally {  
  40.             System.err.println("ThreadName:" + Thread.currentThread().getName() + "    unlock...");  
  41.             writeLock.unlock();  
  42.         }  
  43.     }  
  44.   
  45.     public String read() {  
  46.         readLock.lock();  
  47.         System.out.println("ThreadName:" + Thread.currentThread().getName() + "    lock...");  
  48.         try {  
  49.             System.out.println("ThreadName:" + Thread.currentThread().getName() + "   获取为:" + shareData);  
  50.             Thread.sleep(50);  
  51.         } catch (InterruptedException e) {  
  52.             e.printStackTrace();  
  53.         } finally {  
  54.             readLock.unlock();  
  55.             System.out.println("ThreadName:" + Thread.currentThread().getName() + "    unlock...");  
  56.         }  
  57.         return shareData;  
  58.     }  
  59.   
  60.     /** 
  61.      * @param args 
  62.      */  
  63.     public static void main(String[] args) {  
  64.   
  65.         final ReadWriteLockTest shareData = new ReadWriteLockTest();  
  66.   
  67.         for (int i = 0; i < 5; i++) {  
  68.             new Thread(new Runnable() {  
  69.   
  70.                 @Override  
  71.                 public void run() {  
  72.                     shareData.read();  
  73.                 }  
  74.             }, "get Thread-" + i).start();  
  75.         }  
  76.           
  77.         for (int i = 0; i < 10; i++) {  
  78.             new Thread(new Runnable() {  
  79.                 @Override  
  80.                 public void run() {  
  81.                     shareData.write(new Random().nextLong() + "");  
  82.                 }  
  83.             }, "write Thread-" + i).start();  
  84.   
  85.         }  
  86.     }  
  87. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值