當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。