Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to kill a Thread

 
Jayanthi Mani
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I would just like to get some suggestions about how to kill a
thread. My Server spawns a thread once any client connects to it but
I am unable to stop it once the client exits.
I am using Sockets and have tried using a timeout on the Socket to
see if the thread dies but still it doesn't.
It is basically not coming out of the infinite 'while' loop in
the run method of the 'Thread' class.I have tried a lot of things to
ease the situation but none seem to be yielding any positive results.
Any suggestions would be greatly appreciated. Thanks!
Jayanthi.
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at my answer to a very similar question in the SCJP forum.
Ajith
 
Jerry Pulley
Ranch Hand
Posts: 221
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jayanthi,
The <code>interrupt()</code> method of the <code>Thread</code> class will asynchronously and dependably get a thread's attention. The thread's <code>run()</code> method can use use <code>interrupted()</code> or <code>isInterrupted()</code> to check whether <code>interrupt()</code> was called while the thread was running, and an <code>InterruptedException</code> will be thrown as soon as the thread begins running again if <code>interrupt()</code> was called while the thread was blocked.
jply
 
Jayanthi Mani
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much for ur suggestions. Finally my client exited in a clean manner and all I have to do was really think and change the condition in my 'while' loop (and that is what Sun seemed to recommend, I think)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic