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


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


ReentrantLock 類的 tryLock() 方法僅在調用時任何其他線程不持有該鎖時才持有該鎖。如果當前線程已持有此鎖,則持有計數加 1,該方法返回 true。否則為假。

用法

public boolean tryLock()

public boolean tryLock(long timeout, TimeUnit unit)
                throws InterruptedException

參數

timeout- 等待鎖定的時間

unit- 超時參數的時間單位

返回

如果鎖是空閑的並且被當前線程獲取,則為 true。否則為假。

拋出

InterruptedException - 如果當前線程被中斷

NullPointerException - 如果時間單位為 null

例子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 tryLockClass implements Runnable 
 { 
   String nm; 
   ReentrantLock relc; 
   public tryLockClass(ReentrantLock rl, String n) 
   { 
     relc = rl; 
     nm = n; 
   } 
   public void run() 
   { 
     boolean dn = false; 
     while (!dn) 
     { 
       try {
    	   int c=0;
       if(relc.tryLock()) 
       { 
			int i;
			Thread t1=new Thread();
			for( i =5 ; i <= 10 ; i++)
				System.out.println(t1.getName());
				 t1.checkAccess();
			t1.countStackFrames();
			c=c+1;
			if(i>7)
			{
		System.out.println(t1.getName());
			break;
				
      }
		
      
      }
      } 
		     finally
		     { 
		       //Outer lock release 
		       System.out.println("task name - " + nm + 
		                  " releasing lock(outer lock)"); 
  
		       relc.unlock(); 
		       System.out.println("Lock Hold Count - " + 
		                    relc.getHoldCount()); 
		     } 
		   } 
		   
    } 
  } 

public class ReentrantLocktryLockExample1
 { 
   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 tryLockClass(rel, "Job1"); 
     pool.execute(wrk1); 
     pool.shutdown(); 
   } 
 }

輸出:

Thread-0
Thread-0
Thread-0
Thread-0
Thread-0
Thread-0
Thread-0
task name - Job1 releasing lock(outer lock)
Lock Hold Count - 0

例子2

//import statements

 import java.text.SimpleDateFormat; 
 import java.util.Date; 
 import java.util.concurrent.ExecutorService; 
 import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock; 
   
 class Worker9 implements Runnable 
 { 
   String nm; 
   ReentrantLock relc; 
   public Worker9(ReentrantLock rl, String n) 
   { 
     relc = rl; 
     nm = n; 
   } 
   public void run() 
   { 
     boolean dn = false; 
     while (!dn) 
     { 
    	 TimeUnit unit = TimeUnit.valueOf("DAYS"); 
        
       try {
    	   int c=0;
		if(relc.tryLock(78653, unit)) 
		   { 
			int i;
			Thread t1=new Thread();
			for( i =5 ; i <= 10 ; i++)
				System.out.println(t1.getName());
				 t1.checkAccess();
			t1.countStackFrames();
			c=c+1;
			if(i>7)
			{
		System.out.println(t1.getName());
			break;
				
       }
		
       
       }
       }
		     catch(InterruptedException e) 
		     { 
		       e.printStackTrace(); 
		     } 
		     finally
		     { 
		       //Outer lock release 
		       System.out.println("task name - " + nm + 
		                  " releasing lock(outer lock)"); 
   
		       relc.unlock(); 
		       System.out.println("Lock Hold Count - " + 
		                    relc.getHoldCount()); 
		     } 
		   } 
		   
     } 
   } 
 
   
 public class  ReentrantLocktryLockExample2
 { 
   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 Worker9(rel, "Job1"); 
     Runnable wrk2 = new Worker9(rel, "Job2"); 
     pool.execute(wrk1); 
     pool.execute(wrk2); 
     pool.shutdown(); 
   } 
 }

輸出:

Thread-0
Thread-0
Thread-0
Thread-0
Thread-0
Thread-0
Thread-0
task name - Job1 releasing lock(outer lock)
Lock Hold Count - 0
Thread-1
Thread-1
Thread-1
Thread-1
Thread-1
Thread-1
Thread-1
task name - Job2 releasing lock(outer lock)
Lock Hold Count - 0




相關用法


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