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


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。