• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

Cancelling the Thread Gracefully  RSS feed

 
Ranch Hand
Posts: 113
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
 
Bartender
Posts: 19801
93
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.
 
Ranch Hand
Posts: 104
  • 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: 104
  • 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: 113
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you !!
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!