• Post Reply Bookmark Topic Watch Topic
  • New Topic

Stopping a Thread?  RSS feed

 
Thennam Pandian
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Friends,

I am new to java, I want to stop a thread after some

predefined time period.Please do the needful?
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the thread includes a loop that is continually run, then the looping condition should include a check for a boolean, which can be set from outside the thread through some method. Then create a separate task that does nothing until the timeout limit has been reached, and which then calls that method. This can be done with the java.util.Timer/TimerTask classes; see this example.

If the thread you want to stop does not have a continuously-run loop it gets more complicated. You can use the Thread.stop method, but that is deprecated due to reasons explained in its javadocs; it's possible that those reasons don't apply in your situation, though.
[ November 13, 2006: Message edited by: Ulf Dittmer ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See also this here other thread for some other thoughts.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!