java.util.concurrent类库中提供Condition类来实现线程之间的协调。
调用Condition.await() 方法使线程等待,其他线程调用Condition.signal() 或 Condition.signalAll() 方法唤醒等待的线程。
public class TestObject { private Lock lock = new ReentrantLock(); private Condition condition = lock.newCondition(); public void first() { System.out.println("first is lock"); lock.lock(); try { System.out.println("first is running"); condition.signalAll(); } finally { System.out.println("first is unlock"); lock.unlock(); } } public void second() { System.out.println("second is lock"); lock.lock(); try { condition.await(); System.out.println("second is running"); } catch (InterruptedException e) { e.printStackTrace(); } finally { System.out.println("second is unlock"); lock.unlock(); } } public void third() { System.out.println("third is lock"); lock.lock(); try { condition.await(); System.out.println("third is running"); } catch (InterruptedException e) { e.printStackTrace(); } finally { System.out.println("third is unlock"); lock.unlock(); } } }
public class TestRun { private final static ExecutorService executorService = Executors.newCachedThreadPool(); public static void main(String[] arg) { TestObject to = new TestObject(); executorService.execute(() -> to.third()); executorService.execute(() -> to.second()); executorService.execute(() -> to.first()); } }
third is lock second is lock first is lock first is running first is unlock third is running third is unlock second is running second is unlock
备注:上述实例中,若first方法中改为condition.signal()则second不会被唤醒,需要在third方法的finally中增加condition.signal()才能唤醒second。