ReadWriteLock读写文件

概述

ReadWriteLock是一个接口,在它里面只定义了两个方法:一个读的锁和一个写的锁。

读的锁:A线程获取了读的锁,那么B线程也可以获取读的锁。

写的锁:A线程获取了写的锁,那么B线程不能获取读也不能获取写的锁。

[java]  view plain  copy
  1. public interface ReadWriteLock {  
  2.     /** 
  3.      * Returns the lock used for reading. 
  4.      * 读的锁,A线程获取了读的锁,那么B线程也可以获取读的锁 
  5.      * @return the lock used for reading. 
  6.      */  
  7.     Lock readLock();  
  8.    
  9.     /** 
  10.      * Returns the lock used for writing. 
  11.      * 写的锁,A线程获取了写的锁,那么B线程不能获取读也不能获取写的锁。 
  12.      * @return the lock used for writing. 
  13.      */  
  14.     Lock writeLock();  
  15. }  


代码

[java]  view plain  copy
  1. /** 
  2.  * 使用读写锁,可以实现读写分离锁定,读操作并发进行,写操作锁定单个线程 
  3.  *  
  4.  * 如果有一个线程已经占用了读锁,则此时其他线程如果要申请写锁,则申请写锁的线程会一直等待释放读锁。 
  5.  * 如果有一个线程已经占用了写锁,则此时其他线程如果申请写锁或者读锁,则申请的线程会一直等待释放写锁。 
  6.  * @author 
  7.  * 
  8.  */  
  9. public class MyReentrantReadWriteLock {  
  10.      private ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();  
  11.        
  12.         public static void main(String[] args)  {  
  13.             final MyReentrantReadWriteLock test = new MyReentrantReadWriteLock();  
  14.                
  15.             new Thread(){  
  16.                 public void run() {  
  17.                     test.get(Thread.currentThread());  
  18.                     test.write(Thread.currentThread());  
  19.                 };  
  20.             }.start();  
  21.                
  22.             new Thread(){  
  23.                 public void run() {  
  24.                     test.get(Thread.currentThread());  
  25.                     test.write(Thread.currentThread());  
  26.                 };  
  27.             }.start();  
  28.                
  29.         }    
  30.           
  31.         /** 
  32.          * 读操作,用读锁来锁定 
  33.          * @param thread 
  34.          */  
  35.         public void get(Thread thread) {  
  36.             rwl.readLock().lock();  
  37.             try {  
  38.                 long start = System.currentTimeMillis();  
  39.                    
  40.                 while(System.currentTimeMillis() - start <= 1) {  
  41.                     System.out.println(thread.getName()+"正在进行读操作");  
  42.                 }  
  43.                 System.out.println(thread.getName()+"读操作完毕");  
  44.             } finally {  
  45.                 rwl.readLock().unlock();  
  46.             }  
  47.         }  
  48.   
  49.         /** 
  50.          * 写操作,用写锁来锁定 
  51.          * @param thread 
  52.          */  
  53.         public void write(Thread thread) {  
  54.             rwl.writeLock().lock();;  
  55.             try {  
  56.                 long start = System.currentTimeMillis();  
  57.                    
  58.                 while(System.currentTimeMillis() - start <= 1) {  
  59.                     System.out.println(thread.getName()+"正在进行写操作");  
  60.                 }  
  61.                 System.out.println(thread.getName()+"写操作完毕");  
  62.             } finally {  
  63.                 rwl.writeLock().unlock();  
  64.             }  
  65.         }  
  66. }  

运行结果

读的时候,线程是并行的



写的时候,是单线程的



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值