以下代码抄自java核心技术8卷655页。isbn:978-7-111-23950-5,机械工业出版社。
public void run(){
while(....){
if(suspenndRequested){
suspendLock.lock();
try{while(suspendRequested)suspendCondition.await();}
finally{suspendLock.unlock();}
}
}
}
public void requestSuspend(){suspendRequested=true;}
public void requestResume(){
suspendRequest=false;
suspendLock.lock();
try{suspendCondition.signalAll();}
finally{suspendLock.unlock();}
}
private volatile boolean suspendRequested=false;
private Lock suspendLock = new ReentrantLock();
private Condition suspendCondition = suspendLock.newCondition();