package com.brianway.learning.java.multithread.lock.example8; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.ReentrantLock; /** * Created by Brian on 2016/4/15. */ public class Service2 { public ReentrantLock lock = new ReentrantLock(); private Condition condition = lock.newCondition(); public void waitMethod() { try { lock.lockInterruptibly(); System.out.println("lock begin " + Thread.currentThread().getName()); for (int i = 0; i < Integer.MAX_VALUE / 10; i++) { String s = new String(); Math.random(); } System.out.println("lock end " + Thread.currentThread().getName()); } catch (InterruptedException e) { e.printStackTrace(); } finally { if (lock.isHeldByCurrentThread()) { lock.unlock(); } } } }