当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java ReentrantLock hasQueuedThreads()用法及代码示例


hasQueuedThreads() 返回布尔值 true 或 false,具体取决于是否有任何线程正在等待获取此锁。请注意,因为取消可能随时发生,真正的返回并不能保证任何其他线程将永远获得此锁。

用法

public final boolean hasQueuedThreads()

参数

没有传递参数。

返回

如果可能有其他线程等待获取锁,则为 true

抛出

不会抛出异常。

例子1

import java.util.concurrent.locks.ReentrantLock;

public class ReentrantLockhasQueuedThreadsExample1  {
    public static void main(String[] args) {
 
           ReentrantLock lock=new ReentrantLock();
           MyRunnableClass33 myRnbl=new MyRunnableClass33(lock);
           new Thread(myRnbl,"Thread-1").start();
                     
    }
}
  
class MyRunnableClass33 implements Runnable{
    
    ReentrantLock lock;
    public MyRunnableClass33(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 Threads = "+lock.hasQueuedThreads());
           
           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 Threads = false
Thread-1 has released the lock.

例子2

import java.util.concurrent.locks.ReentrantLock;

public class ReentrantLockhasQueuedThreadsExample2  {
    public static void main(String[] args) {
 
           ReentrantLock lock=new ReentrantLock();
           MyRunnableClass44 myRnbl=new MyRunnableClass44(lock);
           new Thread(myRnbl,"Thread-1").start();
           new Thread(myRnbl,"Thread-2").start();
           new Thread(myRnbl,"Thread-3").start();
           
    }
}
  
 class MyRunnableClass44 implements Runnable{
    
    ReentrantLock lock;
    public MyRunnableClass44(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 Threads = "+lock.hasQueuedThreads());
           
           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.
Thread-2 is Waiting to get the lock
Thread-3 is Waiting to get the lock
 has queued Threads = true
Thread-1 has released  the lock.
Thread-2 has got the  lock.
 has queued Threads = true
Thread-2 has released  the lock.
Thread-3 has got the  lock.
 has queued Threads = false
Thread-3 has released  the lock.




相关用法


注:本文由纯净天空筛选整理自 Java ReentrantLock hasQueuedThreads() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。