读书人

Condition引见

发布时间: 2013-12-11 16:44:13 作者: rapoo

Condition介绍
@Test public void testLock() { //thread main final ReentrantLock rl = new ReentrantLock(); final Condition newCondition = rl.newCondition(); new Thread(new Runnable() { // thread 1 @Override public void run() { rl.lock(); try { Thread.sleep(3000); newCondition.await(); } catch (InterruptedException e) { e.printStackTrace(); } rl.unlock(); } }).start(); new Thread(new Runnable() { //thread 2 @Override public void run() { try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } rl.lock(); newCondition.signal(); rl.unlock(); } }).start(); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } rl.lock(); try { newCondition.await(); } catch (InterruptedException e) { e.printStackTrace(); } rl.unlock(); }

?

?

?

读书人网 >编程

热点推荐