ReentrantLock使用详解(3)之测试锁与超时

来源:互联网 发布:刘道成mysql视频教程 编辑:程序博客网 时间:2024/06/05 14:58

我在上面的这篇博客中,曾经写到过线程在获取锁的过程中是无法中断的。

但是ReentrantLock提供了tryLock()、tryLock(long timeout, TimeUnit unit)、lock.lockInterruptibly()


tryLock() 方法试图申请一个锁,在成功获得锁后返回true,否则,立即返回false,而且线程可以立即离开去做其他的事情。

tryLock(long timeout, TimeUnit unit) 是一个具有超时参数的尝试申请锁的方法,阻塞时间不会超过给定的值;如果成功则返回true

lockInterruptibly() 获得锁,但是会不确定地发生阻塞。如果线程被中断,抛出一个InterruptedException异常。


如果当前获得锁的线程在做大量耗时的工作,使用lock.lock()方法申请锁的线程会一直阻塞,这样就降低了多线程的效率。而使用tryLock()方法申请锁,如果锁不可用则线程不会阻塞,转而可以去做其他工作。代码实例如下:

[java] view plain copy print?
  1. public class ReentrantLockTest4 {  
  2.   
  3.     private ReentrantLock lock = new ReentrantLock();  
  4.       
  5.     public void tryLockTest() throws InterruptedException {  
  6.           
  7.         long beginTime = System.currentTimeMillis();  
  8.         while(System.currentTimeMillis() - beginTime <100) {}  
  9.         // 当前线程尝试获得锁,如果获得锁返回true,否则返回false  
  10.         if(lock.tryLock()) {  
  11.             try{  
  12.                 System.out.println(Thread.currentThread().getName() + " tryLock get lock");  
  13.             } finally {  
  14.                 lock.unlock();  
  15.                 System.out.println(Thread.currentThread().getName() + " tryLock release lock");  
  16.             }  
  17.         } else {  
  18.             System.out.println(Thread.currentThread().getName() + " tryLock can not get lock");  
  19.         }  
  20.           
  21.     }  
  22.       
  23.       
  24.     public void lockTest() {  
  25.         try{  
  26.             // 当前线程在锁可用时直接获得锁,锁不可用时阻塞当前线程  
  27.             lock.lock();  
  28.             System.out.println(Thread.currentThread().getName() + " lock get lock");  
  29.             long beginTime = System.currentTimeMillis();  
  30.             while(System.currentTimeMillis() - beginTime <1000) {}  
  31.         } finally {  
  32.             lock.unlock();  
  33.             System.out.println(Thread.currentThread().getName() + " lock release lock");  
  34.         }  
  35.   
  36.     }  
  37.       
  38.     public static void main(String[] args) {  
  39.         // TODO Auto-generated method stub  
  40.         final ReentrantLockTest4 test = new ReentrantLockTest4();  
  41.         Thread tryLock = new Thread(new Runnable() {  
  42.             public void run() {  
  43.                 try {  
  44.                     test.tryLockTest();  
  45.                 } catch (InterruptedException e) {  
  46.                     // TODO Auto-generated catch block  
  47.                     e.printStackTrace();  
  48.                 }  
  49.             }  
  50.         },"tryLock_thread");  
  51.           
  52.         Thread lock = new Thread(new Runnable() {  
  53.             public void run() {  
  54.                 test.lockTest();  
  55.             }  
  56.         },"lock_thread");  
  57.           
  58.         tryLock.start();  
  59.         lock.start();  
  60.     }  
  61.   
  62. }  
  63.   
  64. 输出结果:  
  65. lock_thread lock get lock  
  66. tryLock_thread tryLock can not get lock  
  67. lock_thread lock release lock  



lock方法不能被中断。如果一个线程在等待获得一个锁时被中断,中断线程在获得锁之前会一直处于 阻塞状态。如果出现死锁,那么lock方法就无法被终止。但是tryLock和lockInterruptibly方法在申请锁的过程中是可以被中断的。代码如下

[java] view plain copy print?
  1. public class ReentrantLockTest5 {  
  2.       
  3.     private ReentrantLock lock = new ReentrantLock();  
  4.       
  5.     public void tryLockInterruptTest() {  
  6.         long beginTime = System.currentTimeMillis();  
  7.         while(System.currentTimeMillis() - beginTime <100) {}  
  8.         try {  
  9.             if (lock.tryLock(5000, TimeUnit.MILLISECONDS)) {  
  10.                 try{  
  11.                     System.out.println(Thread.currentThread().getName() + " tryLock get lock");  
  12.                 }finally {  
  13.                     lock.unlock();  
  14.                 }  
  15.             }  
  16.         } catch (InterruptedException e) {  
  17.             // TODO Auto-generated catch block  
  18.             System.out.println(Thread.currentThread().getName() + " was interrupted");  
  19.         }  
  20.     }  
  21.       
  22.       
  23.     public void lockTest() {  
  24.         try{  
  25.             // 当前线程在锁可用时直接获得锁,锁不可用时阻塞当前线程  
  26.             lock.lock();  
  27.             System.out.println(Thread.currentThread().getName() + " lock get lock");  
  28.             long beginTime = System.currentTimeMillis();  
  29.             while(System.currentTimeMillis() - beginTime <1000) {}  
  30.         } finally {  
  31.             lock.unlock();  
  32.             System.out.println(Thread.currentThread().getName() + " lock release lock");  
  33.         }  
  34.   
  35.     }  
  36.       
  37.     public static void main(String[] args) {  
  38.         // TODO Auto-generated method stub  
  39.         final ReentrantLockTest5 test =  new ReentrantLockTest5();  
  40.         Thread thread_tryLock = new Thread(new Runnable(){  
  41.             @Override  
  42.             public void run() {  
  43.                 // TODO Auto-generated method stub  
  44.                 test.tryLockInterruptTest();  
  45.             }  
  46.         },"tryLockInterruptTest");  
  47.         Thread thread_lock = new Thread(new Runnable(){  
  48.             @Override  
  49.             public void run() {  
  50.                 // TODO Auto-generated method stub  
  51.                 test.lockTest();  
  52.             }  
  53.         },"lockTest");  
  54.         thread_tryLock.start();  
  55.         thread_lock.start();  
  56.         try {  
  57.             Thread.sleep(200);  
  58.         } catch (InterruptedException e) {  
  59.             // TODO Auto-generated catch block  
  60.             System.out.println("main thread was interrupted");  
  61.         }  
  62.         thread_tryLock.interrupt();  
  63.     }  
  64.   
  65. }  
  66.   
  67.   
  68. 输出结果:  
  69. lockTest lock get lock  
  70. tryLockInterruptTest was interrupted  
  71. lockTest lock release lock  

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 创维电视音量小怎么办 捷豹pin码忘记了怎么办 华为手机版本更新下载不了怎么办? 微信钱包没有钱怎么办 微信钱包里没有钱怎么办 微信没有收到退款怎么办 微信转账退款没有收到怎么办 礼物跟人家送重复怎么办 你已被steam封禁怎么办 武装突袭3被锁定怎么办 绝地求生右下角小地图变大怎么办 ctrl z 误删了怎么办 武装突袭3渴了怎么办 武装突袭3枪卡壳怎么办 玩武装突袭3CPU不好怎么办? 户户通001信号中断怎么办 电脑运行速度特别慢怎么办 win8.1电脑太卡怎么办 电线厂非法战地没拆够怎么办 久笔记本电脑玩彩虹六号卡怎么办 彩虹六号一直建立小队进不去怎么办 小佩喂食器离线怎么办 手机打游戏掉帧怎么办 电脑打游戏掉帧怎么办 武装突袭3太卡怎么办 英语b级考不过怎么办 绝地求生被燃烧瓶烧了怎么办 搜狗输入法打字出现问好怎么办 全民k歌解码失败怎么办 视频声音小怎么办调大 乐视2视频声音小怎么办 录视频声音太小怎么办 显卡装了没反应怎么办 笔记本关闭核显黑屏怎么办 驱动补丁被卸了怎么办 网络驱动被删了怎么办 新装系统网卡没驱动怎么办 核显没有dp口怎么办 苹果7屏幕太小了怎么办 苹果装系统卡住了怎么办 苹果7手机白屏黑苹果怎么办