1、犹豫模式
Balking(犹豫)模式用在一个线程发现另一个线程或本线程已经做了某一件相同的事,那么 本线程就无需再做了,直接结束返回。
class Test {
//监控线程
private Thread monitorThread;
//停止标记
private boolean stop = false;
//判断是否执行过start方法
private boolean starting = false;
//启动监控线程
public void start() {
synchronized (this) {
if (starting) {//false
return ;
}
starting = true;
monitorThread = new Thread(……);
monitorThread.start();
}
}
//停止监控线程
public void stop() {
stop = true;
monitorThread.interrupt();
}
}