• Post Reply Bookmark Topic Watch Topic
  • New Topic

Cancelling the Thread Gracefully

 
Raghavendra Desoju
Ranch Hand
Posts: 109
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranchers,

We would like to implement a logic to Cancel a thread execution gracefully. Did anyone try logic similar to below?
Do you have any recommendations?




Thanks, Raghu
 
Tim Holloway
Bartender
Posts: 18408
58
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The cancel method doesn't cancel gracefully. It terminates a thread and allows the thread to attempt to clean up gracefully.

The recommended method to cancel a thread is to use application logic. For example, add a "stopMe()" method to the thread class that sets a "shutdown" flag in the class and let the thread logic interrogate it periodically. If the flag has been set, then the thread should halt what it was doing and clean up.

The difference between this technique and a cancel() is that your termination happens at a controlled point rather than immediately no matter what you are doing. Which generally means that cleanup will be easier.

Of course, if the stopMe() doesn't shutdown the thread in a reasonable length of time, then the thread is probably either hung up on a resource or out of control. THAT's when you cancel() it.
 
Salil Wadnerkar
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Future.cancel does nothing if the task is already in progress, unless you pass "true" (which means interrupt the task if it is running). You can write your task to handle interrupts gracefully by using Thread.isInterrupted() and catching the InterruptedException exception in the sleep. For other blocking wait calls, you need to find out how to end the blocking wait by closing the socket, etc. Have a look at these posts:
https://10kloc.wordpress.com/2013/12/24/cancelling-tasks-in-executors/
https://10kloc.wordpress.com/2013/03/03/java-multithreading-steeplechase-stopping-threads/
 
Salil Wadnerkar
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Salil Wadnerkar wrote:Future.cancel does nothing if the task is already in progress, unless you pass "true" (which means interrupt the task if it is running). You can write your task to handle interrupts gracefully by using Thread.isInterrupted() and catching the InterruptedException exception in the sleep. For other blocking wait calls, you need to find out how to end the blocking wait by closing the socket, etc. Have a look at these posts:
https://10kloc.wordpress.com/2013/12/24/cancelling-tasks-in-executors/
https://10kloc.wordpress.com/2013/03/03/java-multithreading-steeplechase-stopping-threads/

Sorry, I did not see that you are already passing "true". So, you just need to override "interrupt" method to set some error condition that will end the blocking wait in your business logic.
 
Raghavendra Desoju
Ranch Hand
Posts: 109
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you !!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!