在用写爬取网页时,需要程序暂停或者停止。停止可以用强制退出system.exit(0),下面是暂停的主要代码
//false=不暂停 true=暂停
public boolean suspend=false;
public String control = "";
//暂停和继续的公共方法
public boolean setSuspend(boolean suspend) {
boolean flag=true;
synchronized (control) {
try {
if(!suspend){//继续
this.control.notifyAll();
}
} catch (Exception e) {
flag=false;
}
this.suspend = suspend;
}
return flag;
}
//检查是否处于暂停状态,是暂停就等待
synchronized (control) {
if(suspend){
try {
control.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}