Java多线程Lock对象常用方法(2)
来源:互联网 发布:淘宝网充值平台2016 编辑:程序博客网 时间:2024/06/05 14:24
hasQueueThread(Thread thread):方法用于测试thread线程是否在等待当前lock。
hasQueueThreads():方法用于测试是否有现成在等待当前lock。
package com.lenovo.plm.dms.p21;import java.util.concurrent.locks.Condition;import java.util.concurrent.locks.ReentrantLock;public class Service { public ReentrantLock lock = new ReentrantLock(); public Condition condition = lock.newCondition(); public void waitMethod(){ lock.lock(); try { Thread.sleep(Integer.MAX_VALUE); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ lock.unlock(); } }}package com.lenovo.plm.dms.p21;public class Main { public static void main(String[] args) { final Service service = new Service(); Runnable runnable = new Runnable(){ @Override public void run() { // TODO Auto-generated method stub service.waitMethod(); } }; Thread threadA = new Thread(runnable); threadA.start(); try { Thread.sleep(5000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } Thread threadB = new Thread(runnable); threadB.start(); try { Thread.sleep(5000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(service.lock.hasQueuedThread(threadA)); System.out.println(service.lock.hasQueuedThread(threadB)); System.out.println(service.lock.hasQueuedThreads()); } }
测试结果如下:
false
true
true
hasWaiters(Condition condition):方法用于测试是否有现成在等待当前condition对象。
getWaitQueueLength(condition):方法用于测试当前正在等待condition对象的线程数量。
package com.lenovo.plm.dms.p22;import java.util.concurrent.locks.Condition;import java.util.concurrent.locks.ReentrantLock;public class Service { private ReentrantLock lock = new ReentrantLock(); private Condition condition = lock.newCondition(); public void waitMethod(){ lock.lock(); try { System.out.println("To check if has threads waiting condition:"+lock.hasWaiters(condition) + " and thread is: "+ lock.getWaitQueueLength(condition)); condition.await(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ lock.unlock(); } } public void notifyMethod(){ lock.lock(); System.out.println("To check if has threads waiting condition:"+lock.hasWaiters(condition) + " and thread is: "+ lock.getWaitQueueLength(condition)); condition.signal(); lock.unlock(); }}package com.lenovo.plm.dms.p22;public class Main { public static void main(String[] args) { final Service service = new Service(); Runnable runnable = new Runnable(){ @Override public void run() { // TODO Auto-generated method stub service.waitMethod(); } }; Thread[] threadArray = new Thread[10]; for(int i = 0;i<10;i++){ threadArray[i] = new Thread(runnable); } for(int i = 0;i<10;i++){ threadArray[i].start(); } try { Thread.sleep(2000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } service.notifyMethod(); }}
测试结果如下:
To check if has threads waiting condition:false and thread is: 0
To check if has threads waiting condition:true and thread is: 1
To check if has threads waiting condition:true and thread is: 2
To check if has threads waiting condition:true and thread is: 3
To check if has threads waiting condition:true and thread is: 4
To check if has threads waiting condition:true and thread is: 5
To check if has threads waiting condition:true and thread is: 6
To check if has threads waiting condition:true and thread is: 7
To check if has threads waiting condition:true and thread is: 8
To check if has threads waiting condition:true and thread is: 9
To check if has threads waiting condition:true and thread is: 10
- Java多线程Lock对象常用方法(2)
- Java多线程Lock对象常用方法(1)
- Java多线程Lock对象常用方法(3)
- Java多线程Lock对象常用方法(4)
- Java多线程Lock对象之ReentrantLock(2)
- Java多线程----java 对象锁(synchronized/lock)
- Java多线程Lock对象之ReentrantLock(1)
- Java多线程Lock对象之读写锁
- Java多线程探究-Lock锁对象
- java多线程-常用方法
- java多线程常用方法
- Java多线程常用方法
- Java多线程2:Thread常用方法
- Java多线程探究-Lock对象锁条件变量
- java多线程总结2——Lock
- Java多线程---Lock类
- java多线程-Lock
- Java 多线程:Lock 关键字
- 迷途指针
- 如何使域名跳转到个人博客
- java IO 流Stream 序列化Serializable 文件File
- POJ3258-River Hopscotch-二分+贪心【最小值最大化】
- Codeforces Round #343 (Div. 2)C. Famil Door and Brackets DP
- Java多线程Lock对象常用方法(2)
- list泛型,去除对象中某一个字段值重复
- MySQL数据类型
- 【bzoj2502】清理雪道 有上下界的网络流
- softmax回归
- Android Studio 入门指南
- C语言程序设计现代方法第二版基础部分读书笔记(【美】K.N.King 著)
- JSP基本语法、3个编译指令、7个动作指令、9个内置对象
- Mac和Linux文件无法删除的问题