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


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


ReentrantLock 类的 getHoldCount() 方法用于获取当前线程对该锁的持有次数。

用法

public int getHoldCount()

参数

没有传递参数。

返回

当前线程持有此锁的次数,如果当前线程不持有此锁,则为零

抛出

不会抛出异常。

例子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 WorkerClass implements Runnable 
{ 
  String nm; 
  ReentrantLock relc; 
  public WorkerClass(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
        { 
          Date d = new Date(); 
          SimpleDateFormat frmt = new SimpleDateFormat("hh:mm:ss"); 
          System.out.println("task name is- "+ nm 
                     + " outer lock  at "
                     + frmt.format(d) 
                     + " outer work"); 
          Thread.sleep(1500); 
  
          //  Inner Lock 
          relc.lock(); 
          try
          { 
            d = new Date(); 
            frmt = new SimpleDateFormat("hh:mm:ss"); 
            System.out.println("task name is- "+ nm 
                       + " inner lock at "
                       + frmt.format(d) 
                       + " inner work"); 
            System.out.println("Lock Hold Count is - "+ relc.getHoldCount()); 
            Thread.sleep(1500); 
          } 
          catch(InterruptedException e) 
          { 
            e.printStackTrace(); 
          } 
          
          System.out.println("Lock Hold Count - " + relc.getHoldCount()); 
          System.out.println("task name - " + nm + " work done"); 
  
          dn = true; 
        } 
        catch(InterruptedException e) 
        { 
          e.printStackTrace(); 
        } 
        
      }
        
      } 
    } 
  } 
 
  
public class ReentrantLockgetHoldCountExample1
{ 
  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 WorkerClass(rel, "Job1"); 
    pool.execute(wrk1); 
    pool.shutdown(); 
  } 
}

输出:

task name is- Job1 outer lock  at 02:54:52 outer work
task name is- Job1 inner lock at 02:54:56 inner work
Lock Hold Count is - 2
Lock Hold Count - 2
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 worker implements Runnable 
{ 
  String nm; 
  ReentrantLock relc; 
  public worker(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
        { 
          Date dt = new Date(); 
          SimpleDateFormat frmt = new SimpleDateFormat("hh:mm:ss"); 
          System.out.println("task name - "+ nm
                     + " outer lock  at "
                     + frmt.format(dt) 
                     + "  outer work"); 
          Thread.sleep(1500); 
  
          //  Inner Lock 
          relc.lock(); 
          try
          { 
            dt = new Date(); 
            frmt = new SimpleDateFormat("hh:mm:ss"); 
            System.out.println("task name - "+ nm
                       + " inner lock acquired at "
                       + frmt.format(dt) 
                       + " Doing inner work"); 
            System.out.println("Lock Hold Count - "+ relc.getHoldCount()); 
            Thread.sleep(1500); 
          } 
          catch(InterruptedException e) 
          { 
            e.printStackTrace(); 
          } 
          finally
          { 
            //Inner lock release 
            System.out.println("task name - " + nm+ 
                       " releasing lock (inner lock)"); 
  
            relc.unlock(); 
          } 
          System.out.println("Lock Hold Count - " + relc.getHoldCount()); 
          System.out.println("task name - " + nm + " work done"); 
  
          dn = true; 
        } 
        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()); 
        } 
      } 
      else
      { 
        System.out.println("task name - " + nm + 
                      " waiting for lock"); 
        try
        { 
          Thread.sleep(1000); 
        } 
        catch(InterruptedException e) 
        { 
          e.printStackTrace(); 
        } 
      } 
    } 
  } 
} 
  
public class ReentrantLockgetHoldCountExample2
{ 
  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 worker(rel, "Job1"); 
    Runnable wrk2 = new worker(rel, "Job2"); 
    pool.execute(wrk1); 
    pool.execute(wrk2); 
    pool.shutdown(); 
  } 
}

输出:

task name - Job2 waiting for lock
task name - Job1 outer lock  at 02:56:23  outer work
task name - Job2 waiting for lock
task name - Job1 inner lock acquired at 02:56:24 Doing inner work
Lock Hold Count - 2
task name - Job2 waiting for lock
task name - Job2 waiting for lock
task name - Job1 releasing lock (inner lock)
Lock Hold Count - 1
task name - Job1 work done
task name - Job1 releasing lock(outer lock)
Lock Hold Count - 0
task name - Job2 outer lock  at 02:56:27  outer work
task name - Job2 inner lock acquired at 02:56:28 Doing inner work
Lock Hold Count - 2
task name - Job2 releasing lock (inner lock)
Lock Hold Count - 1
task name - Job2 work done
task name - Job2 releasing lock(outer lock)
Lock Hold Count - 0




相关用法


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