如果有线程正在等待这个锁,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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。