我怎样才能使这样的代码工作?
public void start()
{
ThreadPoolManager.getInstance().scheduleWithFixedDelay(new Thread(), 1000, 1000);
}
public class Thread implements Runnable
{
int i = 0;
@Override
public void run()
{
i++;
if(i==5)
//TODO stop this thread
}
}
我想在 i == 5 之后停止线程
编辑:可以这样做:
public void start()
{
ThreadPoolManager.getInstance().schedule(new Thread(), 1000);
}
public class Thread implements Runnable
{
int i = 0;
@Override
public void run()
{
while(i!=5)
{
i++;
try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}
}
}
}
但是,如果有人知道如何使用 scheduleWithFixedDelay 来实现它,我会很高兴知道答案:)