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


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


ReentrantLock 類的 isHeldByCurrentThread() 方法檢查當前線程是否占用該鎖。

用法

public boolean isHeldByCurrentThread()

參數

沒有傳遞參數。

返回

如果當前線程持有此鎖,則為 true,否則為 false

拋出

不會拋出異常。

例子1

import java.text.SimpleDateFormat; 
 import java.util.Date; 
 import java.util.concurrent.ExecutorService; 
 import java.util.concurrent.Executors; 
 import java.util.concurrent.locks.ReentrantLock; 
   
 class WorkerClass21 implements Runnable 
 { 
   String nm; 
   ReentrantLock relc; 
   public WorkerClass21(ReentrantLock rl, String n) 
   { 
     relc = rl; 
     nm = n; 
   } 
   public void run() 
   { 
     boolean dn = false; 
     while (!dn) 
     { 
       //Outer Lock 
       boolean ans = relc.tryLock(); 
   
       //  True if lock is free 
       if(ans) 
       { 
         try
        
         { 
        	 Thread t=new Thread();
        	 
        	 for(int i =0 ; i<=5 ; i++)
        	 {
        		if(i>3){        				
        			System.out.println("    "+i+  "  " +t.getPriority());	
        			t.sleep(1500);
        		}
        		if(i<2) {
        			System.out.println("    "+i+  "  " +t.getName());
        			t.sleep(1500);
        		}
        		
        	 }
           
           relc.lock(); 
         }
         
           catch(InterruptedException e) 
           { 
             e.printStackTrace(); 
           } 
           
           System.out.println(" after sleep(1500) Is held by Current Thread - " + relc.isHeldByCurrentThread()); 
           System.out.println("task name - " + nm + " work done"); 
   
           dn = true; 
         } 
          { 
     
       }       
       } 
     } 
      
   } 
  
   
 public class ReentrantLockisHeldByCurrentThreadExample1 
 { 
   static final int Time = 2; 
   public static void main(String[] args) 
   { 
     ReentrantLock rel = new ReentrantLock(); 
     ExecutorService pool = Executors.newFixedThreadPool(Time); 
     Runnable wrk1 = new WorkerClass21(rel, "Job1"); 
     pool.execute(wrk1); 
     pool.shutdown(); 
   } 
 }

輸出:

    0  Thread-0
    1  Thread-0
    4  5
    5  5
 after sleep(1500) Is held by Current Thread - true
task name - Job1 work done

例子2

import java.text.SimpleDateFormat; 
import java.util.Date; 
import java.util.concurrent.ExecutorService; 
import java.util.concurrent.Executors; 
import java.util.concurrent.locks.ReentrantLock; 
  
class WorkerClass22 implements Runnable 
{ 
  String nm; 
  ReentrantLock relc; 
  public WorkerClass22(ReentrantLock rl, String n) 
  { 
    relc = rl; 
    nm = n; 
  } 
  public void run() 
  { 
    boolean dn = false; 
    while (!dn) 
    { 
      //Outer Lock 
      boolean ans = relc.tryLock(); 
  
      //  True if lock is free 
      if(ans) 
      { 
        try
        { 
        	int i=1;
        	Thread t=new Thread();
          while(i<5) {
        	  
        	  System.out.println("     "+t.getThreadGroup());
        	  if(i==2)
            System.out.println("     "+t.getThreadGroup());
        	  if(i==3)
            System.out.println("     "+t.isInterrupted());
        	  if(i==4)
            System.out.println("     "+t.getStackTrace());
        	  
        	  i=i+1;
          }
        	  
         
  
          assert !relc.isHeldByCurrentThread();
          relc.lock();
                    
          System.out.println(" after sleep(1500) Is held by Current Thread - " + relc.isHeldByCurrentThread()); 
          System.out.println("task name - " + nm + " work done"); 
  
          dn = true; 
        } 
         
        finally {
        	relc.unlock();
        }     
      }     
      } 
    } 
  
  
  } 
 
  
public class ReentrantLockisHeldByCurrentThreadExample2 
{ 
  static final int MAX_Time = 2; 
  public static void main(String[] args) 
  { 
    ReentrantLock rel = new ReentrantLock(); 
    ExecutorService pool = Executors.newFixedThreadPool(MAX_Time); 
    Runnable wrk1 = new WorkerClass22(rel, "Job1"); 
    pool.execute(wrk1); 
    pool.shutdown(); 
  } 
}

輸出:

     java.lang.ThreadGroup[name=main,maxpri=10]
     java.lang.ThreadGroup[name=main,maxpri=10]
     java.lang.ThreadGroup[name=main,maxpri=10]
     java.lang.ThreadGroup[name=main,maxpri=10]
     false
     java.lang.ThreadGroup[name=main,maxpri=10]
     [Ljava.lang.StackTraceElement;@6bd19be0
 after sleep(1500) Is held by Current Thread - true
task name - Job1 work done




相關用法


注:本文由純淨天空篩選整理自 Java ReentrantLock isHeldByCurrentThread() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。