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


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