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


Java ReentrantLock lockInterruptibly()用法及代码示例


ReentrantLock 类的 lockInterruptibly() 方法持有锁,直到或除非当前线程被中断。

用法

public void lockInterruptibly()throws InterruptedException

参数

没有传递参数。

返回

NA

抛出

InterruptedException - 如果当前线程被中断

例子1

import java.util.Date; 
import java.util.concurrent.ExecutorService; 
import java.util.concurrent.Executors; 
import java.util.concurrent.locks.ReentrantLock; 
  
class WorkerClass29 implements Runnable 
{ 
  String nm; 
  ReentrantLock relc; 
  public WorkerClass29(ReentrantLock rl, String n) 
  { 
    relc = rl; 
    nm = n; 
  } 
  public void run() 
  { 
    
    
      //Outer Lock 
      boolean ans = relc.tryLock(); 
  
      //  True if lock is free 
      if(ans) 
      { 
        
        
        int i =0;
       	 Thread t=new Thread();
       	 
       	 for(  i=0 ; i<5 ; i++)
       	 {
       		if(i>3){        				
       			System.out.println("    "+i+  "  " +t.getPriority());
       		
       			
       		}
       		if(i<2) {
       			System.out.println("    "+i+  "  " +t.getName());
       			
       		}
       		
       	 }
      
      } 
     
  }
}
     
public class  ReentrantLocklockInterruptiblyExample1
{ 
  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 WorkerClass29(rel, "Job1"); 
    pool.execute(wrk1); 
    pool.shutdown(); 
  } 
}

输出:

0  Thread-0
1  Thread-0
4  5

例子2

import java.util.concurrent.locks.ReentrantLock;
 
 public class ReentrantLocklockInterruptiblyExample2  {
     public static void main(String[] args) {
  
            ReentrantLock lock=new ReentrantLock();
            MyRunnableClass909 myRnbl=new MyRunnableClass909(lock);
            new Thread(myRnbl,"Thread-1").start();
                        
     }
 }
    
 class MyRunnableClass909 implements Runnable{
     
     ReentrantLock lock;
     public MyRunnableClass909(ReentrantLock lock) { 
            this.lock=lock;
     }
     
     public void run(){
            
            System.out.println(Thread.currentThread().getName()
                         +" is Waiting to get the lock");
            
            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 !lock.isHeldByCurrentThread();
                  lock.lock();
                            
                  System.out.println(" after sleep(1500) Is held by Current Thread - " + lock.isHeldByCurrentThread()); 
                 
                } 
                 
                finally {
                	lock.unlock();
                }       
     }
 }

输出:

Thread-1 is Waiting to get the lock
     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;@53bc4411
 after sleep(1500) Is held by Current Thread - true




相关用法


注:本文由纯净天空筛选整理自 Java ReentrantLock lockInterruptibly() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。