-
}
-
for(int i = 0; i < 100; i++) {
-
System.out.println("main thread seq of " + i + ", loop of " + loop);
-
}
-
bool = true;
-
condition.signal();//this.notify();
-
} finally {
-
lock.unlock();
-
}
-
}
-
public /*synchronized*/ void sub(int loop) throws InterruptedException {
-
lock.lock();
-
try {
-
while(!bool) {
-
condition.await();//this.wait();
-
}
-
for(int i = 0; i < 10; i++) {
-
System.out.println("sub thread seq of " + i + ", loop of " + loop);
-
}
-
bool = false;
-
condition.signal();//this.notify();
-
} finally {
-
lock.unlock();
-
}
-
}
-
}
在Condition中,用await()替换wait(),用signal()替换notify(),用signalAll()替换notifyAll(),传统线程的通信方式,Condition都可以实现,这里注意,Condition是被绑定到Lock上的,要创