Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

alternate of thread.stop

 
roger tiwari
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello
all i m intested to know about the alternate method of thread.stop plz some one help me
thank u
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Roger,

Welcome to JavaRanch!

There are a number of different ways to do this, but basically, you just want to write your run() methods so that they check some condition as they loop, and if the condition changes, they stop. Then you just change the condition to stop the Thread. An example




To stop a thread of this class, you just call "stopMe()" on it.

There are fancier things you can do using the interrupt() and interrupted() methods, but it's the same general idea.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's perfect if doSomething() is fairly fast, which is very often the case. But if it runs a long time the loop might not terminate quickly enough to suit you.

Some run() methods don't loop at all; they just run one long-running task on a separate thread. For these you'll have to try to find a way to kill whatever is taking so long. In some cases there won't be any way to interrupt the task and there just is no neat way to kill the thread. We run into things like this when a remote call to a partner system hangs waiting for a connection or reply.
 
Olaf Kummer
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interrupting a thread is an option when you have to get it out of blocking I/O. In general, be careful which operations you do in a thread that might get interrupted, because there is lots of code out there that does not handle interruptions properly. Take special care, when you use external libraries.

Regards, Olaf
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!