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


Java ReentrantLock hasQueuedThread()用法及代碼示例


如果有線程正在等待這個鎖,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 hasQueuedThread() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。