lock锁常用系列Api
getHoldCount
查询当前线程调用lock锁的次数,请看如下代码
/** * @program: demo * @description: demo * @author: lee * @create: 2018-09-03 **/public class MyService { private ReentrantLock lock ; public MyService() { lock = new ReentrantLock(); } public void serviceMethod() { try { lock.lock(); System.out.println("获得锁的线程名称是" + Thread.currentThread().getName()+"获得此锁锁定的个数是:"+lock.getHoldCount()); serviceMethod2(); } finally { lock.unlock(); } } public void serviceMethod2() { try { lock.lock(); System.out.println("获得锁的线程名称是" + Thread.currentThread().getName()+"获得此锁锁定的个数是:"+lock.getHoldCount()); } finally { lock.unlock(); } }}public class Run { public static void main(String[] args) { final MyService service = new MyService(); Runnable runnable = new Runnable() { @Override public void run() { service.serviceMethod(); } }; new Thread(runnable).start(); }}
输出结果
获得锁的线程名称是Thread-0获得此锁锁定的个数是:1获得锁的线程名称是Thread-0获得此锁锁定的个数是:2
getQueueLength
获得正在等待某个锁释放的线程数,请看如下代码
public class MyService { private ReentrantLock lock ; public MyService() { lock = new ReentrantLock(); } public void serviceMethod() { try { lock.lock(); System.out.println("获得锁的线程名称是" + Thread.currentThread().getName()+"正在等待锁释放的线程数目是:"+lock.getQueueLength()); Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } }}public class Run { public static void main(String[] args) { final MyService service = new MyService(); Runnable runnable = new Runnable() { @Override public void run() { service.serviceMethod(); } }; new Thread(runnable).start(); new Thread(runnable).start(); new Thread(runnable).start(); new Thread(runnable).start(); }}
输出结果
获得锁的线程名称是Thread-0正在等待锁释放的线程数目是:0获得锁的线程名称是Thread-1正在等待锁释放的线程数目是:2获得锁的线程名称是Thread-2正在等待锁释放的线程数目是:1获得锁的线程名称是Thread-3正在等待锁释放的线程数目是:0
getWaitQueueLength
获取某个锁下condition执行等待的次数,请看如下代码
public class MyService { private ReentrantLock lock = new ReentrantLock(); private Condition condition = lock.newCondition(); public MyService(){} public void serviceMethod() { try { lock.lock(); System.out.println("获得锁的线程名称是" + Thread.currentThread().getName() + "condition执行await的次数是:" + lock.getWaitQueueLength(condition)); condition.await(); Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } }}public class Run { public static void main(String[] args) { final MyService service = new MyService(); Runnable runnable = new Runnable() { @Override public void run() { service.serviceMethod(); } }; new Thread(runnable).start(); new Thread(runnable).start(); new Thread(runnable).start(); new Thread(runnable).start(); }}
输出结果
获得锁的线程名称是Thread-0condition执行await的次数是:0获得锁的线程名称是Thread-2condition执行await的次数是:1获得锁的线程名称是Thread-1condition执行await的次数是:2获得锁的线程名称是Thread-3condition执行await的次数是:3
getWaitQueueLength
该方法主要的作用就是获取线程对某个锁的condition中的await调用了多少次
public class MyService { private ReentrantLock lock = new ReentrantLock(); ; private Condition condition = lock.newCondition(); public MyService(){ } public void serviceMethod() { try { lock.lock(); System.out.println("获得锁的线程名称是" + Thread.currentThread().getName() + "condition执行await的次数是:" + lock.getWaitQueueLength(condition)); condition.await(); Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } }}public class Run { public static void main(String[] args) { final MyService service = new MyService(); Runnable runnable = new Runnable() { @Override public void run() { service.serviceMethod(); } }; new Thread(runnable).start(); new Thread(runnable).start(); new Thread(runnable).start(); new Thread(runnable).start(); }}
输出结果
获得锁的线程名称是Thread-0condition执行await的次数是:0获得锁的线程名称是Thread-1condition执行await的次数是:1获得锁的线程名称是Thread-2condition执行await的次数是:2获得锁的线程名称是Thread-3condition执行await的次数是:3
hasQueuedThread
查询指定线程是否正在等待获取某个锁的锁定
public class MyService { public ReentrantLock lock = new ReentrantLock(); ; public Condition condition = lock.newCondition(); public MyService(){ } public void serviceMethod() { try { lock.lock(); System.out.println("获得锁的线程名称是" + Thread.currentThread().getName() + "condition执行await的次数是:" + lock.getWaitQueueLength(condition)); Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } }}public class Run { public static void main(String[] args) throws InterruptedException { final MyService service = new MyService(); Runnable runnable = new Runnable() { @Override public void run() { service.serviceMethod(); } }; Thread thread =new Thread(runnable); thread.start(); Thread.sleep(1000); System.out.println(service.lock.hasQueuedThread(thread)); Thread thread1 =new Thread(runnable); thread1.start(); Thread.sleep(1000); System.out.println(service.lock.hasQueuedThread(thread1)); }}
输出结果
获得锁的线程名称是Thread-0condition执行await的次数是:0falsetrue获得锁的线程名称是Thread-1condition执行await的次数是:0
hasQueuedThreads
查询是否有线程正在等待此锁的锁定,请看如下代码
public class Run { public static void main(String[] args) throws InterruptedException { final MyService service = new MyService(); Runnable runnable = new Runnable() { @Override public void run() { service.serviceMethod(); } }; Thread thread =new Thread(runnable); thread.start(); Thread.sleep(1000); System.out.println(service.lock.hasQueuedThread(thread)); Thread thread1 =new Thread(runnable); thread1.start(); Thread.sleep(1000); System.out.println(service.lock.hasQueuedThreads()); }}
输出结果
获得锁的线程名称是Thread-0condition执行await的次数是:0falsetrue获得锁的线程名称是Thread-1condition执行await的次数是:0
hasWaiters
判断是否有线程正在等待与此锁有关的condition条件。请看代码
public class MyService { public ReentrantLock lock = new ReentrantLock(); ; public Condition condition = lock.newCondition(); public MyService(){ } public void serviceMethod() { try { lock.lock(); System.out.println("获得锁的线程名称是" + Thread.currentThread().getName() + "condition执行await的次数是:" + lock.getWaitQueueLength(condition)); System.out.println(lock.hasWaiters(condition)); condition.await(); } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } }}public class Run { public static void main(String[] args) throws InterruptedException { final MyService service = new MyService(); Runnable runnable = new Runnable() { @Override public void run() { service.serviceMethod(); } }; Thread thread =new Thread(runnable); thread.start(); Thread.sleep(1000); Thread thread1 =new Thread(runnable); thread1.start(); Thread.sleep(1000); }}
输出结果
获得锁的线程名称是Thread-0condition执行await的次数是:0false获得锁的线程名称是Thread-1condition执行await的次数是:1true
isHeldByCurrentThread
锁是否被当前线程锁定
public class MyService { public ReentrantLock lock = new ReentrantLock(); ; public Condition condition = lock.newCondition(); public MyService(){ } public void serviceMethod() { try { System.out.println("锁是否被当前线程锁定"+lock.isHeldByCurrentThread()); lock.lock(); System.out.println("锁是否被当前线程锁定"+lock.isHeldByCurrentThread()); } finally { lock.unlock(); } }}
输出结果
锁是否被当前线程锁定false锁是否被当前线程锁定true
isLocked
锁是否被任意线程锁定
public class MyService { public ReentrantLock lock = new ReentrantLock(); ; public Condition condition = lock.newCondition(); public MyService(){ } public void serviceMethod() { try { System.out.println("锁是否被任意线程锁定"+lock.isLocked()); lock.lock(); System.out.println("锁是否被任意线程锁定"+lock.isLocked()); } finally { lock.unlock(); } }}
lockInterruptibly
如果当前线程未获得中断,则加锁,否则出现异常。
public class MyService { public ReentrantLock lock = new ReentrantLock(); ; public Condition condition = lock.newCondition(); public MyService() { } public void serviceMethod() { try { System.out.println(Thread.currentThread().getName()+"锁是否被当前线程锁定" + lock.isLocked()); lock.lockInterruptibly(); System.out.println(Thread.currentThread().getName()+"锁是否被当前线程锁定" + lock.isLocked()); } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } }}public class Run { public static void main(String[] args) throws InterruptedException { final MyService service = new MyService(); Runnable runnable = new Runnable() { @Override public void run() { service.serviceMethod(); } }; Thread thread =new Thread(runnable); thread.start(); Thread thread2 =new Thread(runnable); thread2.start(); thread2.interrupt(); }}
输出结果
java.lang.InterruptedException at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireInterruptibly(AbstractQueuedSynchronizer.java:1219) at java.util.concurrent.locks.ReentrantLock.lockInterruptibly(ReentrantLock.java:340) at chaprer3.MyService.serviceMethod(MyService.java:26) at chaprer3.Run$1.run(Run.java:16) at java.lang.Thread.run(Thread.java:744)Exception in thread "Thread-1" java.lang.IllegalMonitorStateException at java.util.concurrent.locks.ReentrantLock$Sync.tryRelease(ReentrantLock.java:155) at java.util.concurrent.locks.AbstractQueuedSynchronizer.release(AbstractQueuedSynchronizer.java:1260) at java.util.concurrent.locks.ReentrantLock.unlock(ReentrantLock.java:460) at chaprer3.MyService.serviceMethod(MyService.java:31) at chaprer3.Run$1.run(Run.java:16) at java.lang.Thread.run(Thread.java:744)Thread-1锁是否被当前线程锁定falseThread-0锁是否被当前线程锁定falseThread-0锁是否被当前线程锁定true
tryLock
锁未被另一个线程占用的时候进行加锁
public class MyService { public ReentrantLock lock = new ReentrantLock(); ; public Condition condition = lock.newCondition(); public MyService() { } public void serviceMethod() { if (lock.tryLock()){ System.out.println(Thread.currentThread().getName()+":获得锁"); }else { System.out.println(Thread.currentThread().getName()+"未获得锁"); } }}public class Run { public static void main(String[] args) throws InterruptedException { final MyService service = new MyService(); Runnable runnable = new Runnable() { @Override public void run() { service.serviceMethod(); } }; Thread thread =new Thread(runnable); thread.start(); Thread thread2 =new Thread(runnable); thread2.start(); }}
输出结果
Thread-0:获得锁Thread-1未获得锁
###tryLock(long timeout, TimeUnit unit) 如果锁在给定的时间内没有被别的锁加锁并且当前线程没有被中断,则加锁。
/** * @program: demo * @description: demo * @author: lee * @create: 2018-09-03 **/public class MyService { public ReentrantLock lock = new ReentrantLock(); ; public Condition condition = lock.newCondition(); public MyService() { } public void serviceMethod() { try { //如果线程在3秒内没有被另一个锁加锁并且当前线程没有被中断,则加锁 if (lock.tryLock(3, TimeUnit.SECONDS)){ System.out.println(Thread.currentThread().getName()+":获得锁"); }else { System.out.println(Thread.currentThread().getName()+"未获得锁"); } } catch (InterruptedException e) { e.printStackTrace(); } }}public class Run { public static void main(String[] args) throws InterruptedException { final MyService service = new MyService(); Runnable runnable = new Runnable() { @Override public void run() { service.serviceMethod(); } }; Thread thread =new Thread(runnable); thread.start(); Thread thread2 =new Thread(runnable); thread2.start(); }}
输出结果
Thread-1:获得锁Thread-0未获得锁
awaitUntil(Date deadline) throws InterruptedException;
在规定时间出唤醒线程
public class MyService { public ReentrantLock lock = new ReentrantLock(); ; public Condition condition = lock.newCondition(); public MyService() { } public void serviceMethod() { try { Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.SECOND, 10); lock.lock(); long start =System.currentTimeMillis(); System.out.println("lock begin:" + start); condition.awaitUntil(calendar.getTime()); long end =System.currentTimeMillis(); System.out.println("lock end:" + end); System.out.println(end-start); } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } }}public class Run { public static void main(String[] args) throws InterruptedException { final MyService service = new MyService(); Runnable runnable = new Runnable() { @Override public void run() { service.serviceMethod(); } }; Thread thread =new Thread(runnable); thread.start(); }}
输出结果
lock begin:1536140927367lock end:153614093736810001