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


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


ReentrantLock 类的 isLocked() 方法检查是否有任何线程占用此锁。创建此方法是为了监视系统状态,而不是用于同步控制。

用法

public boolean isLocked()

参数

没有传递参数。

返回

如果任何线程持有此锁,则为 true,否则为 false

抛出

不抛出异常

例子1

//import statement

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 WorkerClass23 implements Runnable 
{ 
  String nm; 
  ReentrantLock relc; 
  public WorkerClass23(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=0;
          for(i=0;i<=6;i++) {
        	  
        	  if(i>=2)
        	  {
        		  Thread t = new Thread();
        		  System.out.println("Thread Created.....");
        		  if(i==3) {
        			  t.setName("Maint Thread2");
        			  System.out.println("Thread Created.....");
        		  }
        		
        		  
        	  }
        	  if(i==4)
        		  break;
        	  
          }
          
          System.out.println(" after sleep(1500) Is locked - " + relc.isLocked()); 
          System.out.println("task name - " + nm + " work done"); 
  
          dn = true; 
        } 
        catch(Exception e) 
        { 
          e.printStackTrace(); 
        } 
        
      } 
      } 
    } 
  } 
 
  
public class ReentrantLockisLockedExample1 
{ 
  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 WorkerClass23(rel, "Job1"); 
    pool.execute(wrk1); 
    pool.shutdown(); 
  } 
}

输出:

Thread Created.....
Thread Created.....
Thread Created.....
Thread Created.....
 after sleep(1500) Is locked - true
task name - Job1 work done

例子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.locks.ReentrantLock; 
  
class WorkerClass24 implements Runnable 
{ 
  String nm; 
  ReentrantLock relc; 
  public WorkerClass24(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;
          }
        	  
         if(relc.isLocked())
         {
        	 
         }
         else {
        	 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 ReentrantLockisLockedExample2 
{ 
  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 WorkerClass24(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;@3127c164
 after sleep(1500) Is held by Current Thread - true
task name - Job1 work done




相关用法


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