如果有線程正在等待這個鎖,ReentrantLock 類的 hasQueuedThread(Thread thread) 方法返回 true。請注意,因為取消可能隨時發生,真正的返回並不能保證該線程將永遠獲得此鎖。該方法主要用於監控係統狀態。
用法
public final boolean hasQueuedThread(Thread thread)
參數
線程 - 線程
返回
如果給定線程排隊等待此鎖,則為 true
拋出
NullPointerException - 如果線程為空。
例子1
//import statement
package com.app.Rentrant;
import java.util.concurrent.locks.ReentrantLock;
public class ReentrantLockhasQueuedThreadExample1 {
public static void main(String[] args) {
ReentrantLock lock=new ReentrantLock();
MyRunnableClass3 myRnbl=new MyRunnableClass3(lock);
new Thread(myRnbl,"Thread-1").start();
}
}
class MyRunnableClass3 implements Runnable{
ReentrantLock lock;
public MyRunnableClass3(ReentrantLock lock) {
this.lock=lock;
}
public void run(){
System.out.println(Thread.currentThread().getName()
+" is Waiting to get the lock");
lock.lock();
System.out.println(Thread.currentThread().getName()
+" has got the lock.");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(" has queued Thread = "+lock.hasQueuedThread(Thread.currentThread()));
System.out.println(Thread.currentThread().getName()
+" has released the lock.");
lock.unlock(); //read explanation for 5sec
}
}
輸出:
Thread-1 is Waiting to get the lock Thread-1 has got the lock. has queued Thread = false Thread-1 has released the lock.
例子2
//import statements
package com.app.Rentrant;
import java.util.concurrent.locks.ReentrantLock;
public class ReentrantLockhasQueuedThreadExample2 {
public static void main(String[] args) {
ReentrantLock lock=new ReentrantLock();
MyRunnableClass4 myRnbl=new MyRunnableClass4(lock);
new Thread(myRnbl,"Thread-1").start();
new Thread(myRnbl,"Thread-2").start();
new Thread(myRnbl,"Thread-3").start();
}
}
class MyRunnableClass4 implements Runnable{
ReentrantLock lock;
public MyRunnableClass4(ReentrantLock lock) {
this.lock=lock;
}
public void run(){
System.out.println(Thread.currentThread().getName()
+" is Waiting to get the lock");
lock.lock();
System.out.println(Thread.currentThread().getName()
+" has got the lock.");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(" has queued Thread = "+lock.hasQueuedThread(Thread.currentThread()));
System.out.println(Thread.currentThread().getName()
+" has released the lock.");
lock.unlock(); //read explanation for 5sec
}
}
輸出:
Thread-2 is Waiting to get the lock Thread-3 is Waiting to get the lock Thread-1 is Waiting to get the lock Thread-2 has got the lock. has queued Thread = false Thread-2 has released the lock. Thread-3 has got the lock. has queued Thread = false Thread-3 has released the lock. Thread-1 has got the lock. has queued Thread = false Thread-1 has released the lock.
相關用法
- Java ReentrantLock hasQueuedThreads()用法及代碼示例
- Java ReentrantLock lock()用法及代碼示例
- Java ReentrantLock toString()用法及代碼示例
- Java ReentrantLock tryLock()用法及代碼示例
- Java ReentrantLock isHeldByCurrentThread()用法及代碼示例
- Java ReentrantLock isLocked()用法及代碼示例
- Java ReentrantLock getHoldCount()用法及代碼示例
- Java ReentrantLock isFair()用法及代碼示例
- Java ReentrantLock getOwner()用法及代碼示例
- Java ReentrantLock lockInterruptibly()用法及代碼示例
- Java Reentrant getQueueLength()用法及代碼示例
- Java Reader read(CharBuffer)用法及代碼示例
- Java Reader markSupported()用法及代碼示例
- Java Reader ready()用法及代碼示例
- Java ResolverStyle valueOf()用法及代碼示例
- Java ResourceBundle containsKey()用法及代碼示例
- Java ResourceBundle getString()用法及代碼示例
- Java ResourceBundle getKeys()用法及代碼示例
- Java ResourceBundle clearCache()用法及代碼示例
- Java Reader close()用法及代碼示例
注:本文由純淨天空篩選整理自 Java ReentrantLock hasQueuedThread() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。