本文內容部分引自《Java多線程編程核心技術》,感謝作者!!!
代碼地址:https://github.com/xianzhixianzhixian/thread.git
方法hasQueueThread(Thread thread)、hasQueueThreads()和hasWaiters(Condition condition)的用法和區別
1、方法lock.hasQueueThread(Thread thread)的作用是查詢指定的線程是否在等待獲取此鎖,就是當前該線程是否處於等待獲取該鎖的狀態
2、方法lock.hasQueueThreads()的作用是檢測當前是否有線程在等待獲取該鎖
3、方法lock.hasWaiters(Condition condition)的作用是檢測當前是否有線程已調用condition.await()並且處於await狀態
方法hasQueueThread(Thread thread)、hasQueueThreads()用法示例
Service.java
/**
* 方法lock.hasQueueThread(Thread thread)的作用是查詢指定的線程是否在等待獲取此鎖定,就是當前該線程是否處於未獲取鎖的狀態
* 方法lock.hasQueueThreads()的作用是檢測當前是否有線程在等待獲取該鎖定
* @author: xianzhixianzhixian
* @date: 2019-01-16 20:15
*/
public class Service {
public ReentrantLock lock = new ReentrantLock();
public Condition condition = lock.newCondition();
public void lockMethod(){
try {
lock.lock();
Thread.sleep(Integer.MAX_VALUE);
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}
Run.java
/**
* @author: xianzhixianzhixian
* @date: 2019-01-16 20:37
*/
public class Run {
public static void main(String[] args) throws Exception {
final Service service = new Service();
Runnable runnable = new Runnable() {
@Override
public void run() {
service.lockMethod();
}
};
Thread threadA = new Thread(runnable);
threadA.start();
Thread.sleep(500);
Thread threadB = new Thread(runnable);
threadB.start();
Thread.sleep(500);
System.out.println(service.lock.hasQueuedThread(threadA));
System.out.println(service.lock.hasQueuedThread(threadB));
System.out.println(service.lock.hasQueuedThreads());
}
}
運行結果:線程A先啓動佔用了鎖,線程B則一直處於等待鎖的狀態。所以第一行是false,第二行是true。當前有線程在等待鎖,第三行爲true。
方法hasWaiters(Condition condition)用法示例
ServiceHasWaiters.java
/**
* 方法lock.hasWaiters(Condition condition)的作用是檢測當前是否有線程已調用condition.await()並且處於await狀態
* @author: xianzhixianzhixian
* @date: 2019-01-16 21:19
*/
public class ServiceHasWaiters {
private ReentrantLock lock = new ReentrantLock();
private Condition condition = lock.newCondition();
public void waitMethod(){
try {
lock.lock();
condition.await();
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void notifyMethod(){
try {
lock.lock();
System.out.println("有沒有線程正在等待condition?"
+lock.hasWaiters(condition)+"線程數是多少?"
+lock.getWaitQueueLength(condition));
condition.signal();
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}
Run.java
/**
* @author: xianzhixianzhixian
* @date: 2019-01-16 20:37
*/
public class Run {
public static void main(String[] args) throws Exception {
final ServiceHasWaiters serviceHasWaiters = new ServiceHasWaiters();
Runnable runnableHasWaiters = new Runnable() {
@Override
public void run() {
serviceHasWaiters.waitMethod();
}
};
Thread[] threadArray = new Thread[10];
for (int i = 0; i < 10; i++) {
threadArray[i] = new Thread(runnableHasWaiters);
}
for (int i = 0; i < 10; i++) {
threadArray[i].start();
serviceHasWaiters.notifyMethod();
}
Thread.sleep(2000);
serviceHasWaiters.notifyMethod();
}
}
運行結果:因爲線程在start之後有可能會被立即喚醒,所以結果中出現了爲0爲1的情況,這也證實了lock.hasWaiters(Condition condition)統計的是是否有處於wait狀態的線程。