• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to kill a thread when it times out

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have a question regarding destroying a thread. I know destroy() method is not implemented and want to know how this can be done. Consider
the following case.



Output:

running i = 0
timed out - I cannot wait further
running i = 1
running i = 2
running i = 3
running i = 4
running i = 5
running i = 6
running i = 7
running i = 8
...

I want to gracefully kill the t thread then join operation times out.

thanks for your help.

Cheers
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Laiq Ahmed wrote:
I want to gracefully kill the t thread then join operation times out.



The recommended way to "kill" a thread, is to do so cooperatively. Meaning...

The thread needs to have a flag that is check periodically. And when set, the thread will terminate. Also, setting a flag isn't enough. Files and sockets needs to be closed in order to have io operations terminate. The thread needs to be interrupted in order to have sleep() and wait() operations terminate.

So, you will need to write some sort of kill() method for your thread which will set a flag, interrupt the thread, and close resources. This thread will also need to check this flag, when it can. And deal with the IO and interuppted exceptions, to check the flag. And if the flag is set, clean up after itself, and terminate.

Henry

 
Ranch Hand
Posts: 400
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hope this code helps you..

 
Laiq Ahmed
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys. I thought I could kill a thread by calling interrupt() on it.
Thanks for making this clear.

Cheers.
 
Minhaj Mehmood
Ranch Hand
Posts: 400
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
your are most welcome.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic