java 程序暂停_java程序运行过程中如何暂停,恢复?
展開全部
java控制程序執行,使用的是Thread這個類,62616964757a686964616fe78988e69d8331333337613132可以控制程序暫停或者休眠幾秒再執行。示例如下:public?abstract?class?MyThread?extends?Thread?{
private?boolean?suspend?=?false;
private?String?control?=?"";?//?只是需要一個對象而已,這個對象沒有實際意義
public?void?setSuspend(boolean?suspend)?{
if?(!suspend)?{
synchronized?(control)?{
control.notifyAll();
}
}
this.suspend?=?suspend;
}
public?boolean?isSuspend()?{
return?this.suspend;
}
public?void?run()?{
while?(true)?{
synchronized?(control)?{
if?(suspend)?{
try?{
control.wait();
}?catch?(InterruptedException?e)?{
e.printStackTrace();
}
}
}
this.runPersonelLogic();
}
}
protected?abstract?void?runPersonelLogic();
public?static?void?main(String[]?args)?throws?Exception?{
MyThread?myThread?=?new?MyThread()?{
protected?void?runPersonelLogic()?{
System.out.println("myThead?is?running");
}
};
myThread.start();
Thread.sleep(3000);
myThread.setSuspend(true);
System.out.println("myThread?has?stopped");
Thread.sleep(3000);
myThread.setSuspend(false);
}
}
總結
以上是生活随笔為你收集整理的java 程序暂停_java程序运行过程中如何暂停,恢复?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Centos7 搭建 Svn+Revie
- 下一篇: 常见游戏外挂分类及原理概述