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(); }
?
?
?