62,621
社区成员
发帖
与我相关
我的任务
分享
public class ScheduledTask implements Runnable {
public void startService(){
run();
}
public void run()
{
synchronized(this)
{
while(!this.flag)
{
try
{
wait();
}
catch(InterruptedException e)
{
}
}
}
try
{
//线程主操作
}
catch (Exception e)
{
e.printStackTrace();
}
}
public synchronized void stop()
{
this.flag = false;
}
public synchronized void restart()
{
this.flag = true;
notify();
}
for(;;)
{
if(主线程开关)
{
.... // 有一个阻塞的方法,会阻塞的方法都有个特点,就是会抛出 InterruptedException 异常。
}
}
for(;;)
{
if(主线程开关)
{
//操作
}
}