This week's book giveaway is in the Beginning Java forum.
We're giving away four copies of Get Programming with Java (MEAP only) and have Peggy Fisher on-line!
See this thread for details.
Win a copy of Get Programming with Java (MEAP only) this week in the Beginning Java forum!
  • 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
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

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: 20153
104
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 !!
 
The moth suit and wings road is much more exciting than taxes. Or this tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!