• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

Killing Threads

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a thread which is started when its outer class is instantiated. At any given time, this thread likely to be blocked by a DatagramSocket.read() call, and it is in an infinite loop. I want to write a method called close() which, among other things, will kill this thread. My first thought was for the loop condition to be an instance variable which could be modified. Naturally, this cannot be the case as if the thread has been blocked, it may never get back to test this variable (and subsequently break the loop).

Is there a way to cause a thread to stop what its doing and go into a cleanup routine?
 
Ranch Hand
Posts: 489
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you are on the right track with the idea of using the isnatnce variable to check for a certain status. Once the thread gets into the loop, it should be able to complete it. Are you saying that it gets blcoked midway? Posting some code to show why this is not feasible may help.

ram.
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, your loop could be somewhat like this:



When the condition to stop the thread happens, simply call the interrupt() method of the thread.

As for the socket you can set a time out using the setSoTimeout() method in the Socket class. Once the time out has elapsed, an InterruptedException will be thrown by the read() method of the InputStream.

As you have to catch that InterruptedException, in the catch block you could simple call the interrupt method again.
[ May 09, 2006: Message edited by: Edwin Dalorzo ]
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure the OP meant "infinite loop" literally. It is possible to have a thread that doesn't even have a loop hung eternally on some blocking operation. Frinstance, HttpURLConnection does not have a timeout parameter or a reliable way to kill it and you can wind up hung forever waiting for a response.

Then again, the WebSphere EJB container can kill ANYTHING after its max transaction time. I wonder how they do that.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> Then again, the WebSphere EJB container can kill ANYTHING after its > max transaction time. I wonder how they do that.

Possibly a non-standard feature in the IBM JVM.
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nonstandard JVM would not surprise me. When the container times us out we see an exception in the log but we cannot catch it and continue. My working theory is that the JVM does not give my thread one more CPU cycle period. It just says you've had your two minutes and you're outta here!
 
I got this tall by not having enough crisco in my diet as a kid. This ad looks like it had plenty of shortening:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic