Is there a way to completely stop a task in java?
Is there a way to completely stop a task in java?
I want a task to run at scheduled interval and timeout if it does not complete in required time and continue further iteration.
I have gone through the following answers, but it doesn't address my problem..
How do you kill a Thread in Java?
ExecutorService that interrupts tasks after a timeout
Consider the following case
BasicThreadFactory collectionFactory = new BasicThreadFactory.Builder()
.namingPattern("CollectionExecutor-%d")
.build();
// thread pool size is set 2
// 1-for scheduler thread which runs task and tracks timeout
// 2-for task itself
ScheduledExecutorService collectionExecuter =
Executors.newScheduledThreadPool(2, collectionFactory);
// fires collection every minute and if it is in between execution during
// scheduled time then waits for completion and executes immediately
// after it
//my task:
Runnable runnable= new Runnable()
@Override
public void run()
try
System.out.println("Executed started");
Thread.sleep(2000);
System.out.println("Executed after .get method call.");
catch (InterruptedException e)
e.printStackTrace();
try
Thread.sleep(20000);
System.out.println("Executed even after .cancel method " +
"call (I want this to avoid this.)");
catch (Exception e)
e.printStackTrace();
;
Above task should run with an interval of 3 sec and stop if it takes more than 1 sec...Consider It is not possible to have complete task in single try catch block, now how could I stop the task to wait further in next sleep(20000) and continue with next iteration.
collectionExecuter.scheduleAtFixedRate(new Runnable() //scheduler thread
@Override
public void run()
try
Future<?> future = collectionExecuter.submit(runnable);
try
future.get(1, TimeUnit.SECONDS);
catch (Exception e)
future.cancel(true);
System.out.println("Collection thread did not " +
"completed in 1 Sec.Thread Interrupted");
catch (Exception e)
System.out.println("Unable to start Collection Thread");
, 0, 3, TimeUnit.SECONDS);
}
Why did those other questions not help? I see several answers there that would achieve the behavior you desire.
– Max Vollmer
Aug 28 at 10:59
@Thomas sleep could a DB call which has a read latency of let say 20 sec.
– atul
Aug 28 at 10:59
If your problem is caused by blocking IO calls, your question should be about asynchronous IO implementations, not about killing threads. See this for example: stackoverflow.com/questions/4087696/…
– Max Vollmer
Aug 28 at 11:01
All InterruptedException catch should stop properly your thread, Ever relaunch currentThread.interrupt(), or in your case, simply return. When you catch this exception, you also catch the interrupt signal. a good answer on this: stackoverflow.com/questions/3976344/…
– pdem
Aug 28 at 11:07
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Have the task check some flag after the long sleep and return if that flag indicates the task should stop.
– Thomas
Aug 28 at 10:57