设计模式-犹豫模式(Balking)
Balking模式用在一个线程发现另一个线程或本地线程在重复做某件相同的事,那么就让本地线程不用再做了直接结束返回。
class MonitorService{
private Thread t1;
private boolean i;
private boolean n=false;
public void start(){
synchronized (this){
if(n){
return;
}
n=true;
}
t1=new Thread(()->{
i=true;
while (i){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("监控线程启动");
}
});
t1.start();
}
public void stop(){
i=false;
t1.interrupt();
}
}

浙公网安备 33010602011771号