how can i make such code working?
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 want to stop the Thread after i == 5
Edit:
It can be done like that:
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();}
}
}
}
But still if anybody have idea how to make it with scheduleWithFixedDelay i would be glad to know the answer 🙂
Stopping a
ScheduledThreadPoolExecutortask from within itself is a bit more convoluted. You could try to pass back theScheduledFutureto the task itself and callcancel(not thread-safe but since you have a delay of 1000 ms it should be enough):