• Post Reply Bookmark Topic Watch Topic
  • New Topic

Killing Threads  RSS feed

 
Tristan McHardie
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • 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?
 
ramprasad madathil
Ranch Hand
Posts: 489
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • 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.
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • 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 ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • 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.
 
Ajay A Patil
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • 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)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • 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!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!