Java threads

How to terminate a running thread using code ?i'm not sure if the following methods work properly :destroy();wait();notify();notifyAll();i tried to use them but none of them worked as expectedplz any help ?
[270 byte] By [mkashef] at [2007-9-27 14:20:13]
# 1
Just let it die. The real question is how do you keep it alive... A thread dies when its run method returns. Its associated object may not disappear, but you can't run it again.
dnoyeB at 2007-7-5 22:10:15 > top of Java-index,Core,Core APIs...
# 2
Try showing some duke dollars and you will get a better response.
dnoyeB at 2007-7-5 22:10:15 > top of Java-index,Core,Core APIs...
# 3

the accepted way for thread X to terminate thread Y is to provide a method in Y, MyStopMethod, that knows how to terminate itself using flags and proper "waking". do something like this:

void run()

{

while ( continueProcessing )

{

// do something

}

} // run

void MyStopMethod()

{

continueProcessing = false;

// notify thread if necessary to "wake it up"

} // MyStopMethod

it is not recommended to use the "stop" method in the Thread class, and in fact, it has been deprecated in java 2.

btoddb at 2007-7-5 22:10:15 > top of Java-index,Core,Core APIs...
# 4
The interrupt() method is the acceptable method to call.
andyba at 2007-7-5 22:10:15 > top of Java-index,Core,Core APIs...
# 5
> The interrupt() method is the acceptable method to> call.If your want to interrupt the thread, but not acceptable if you want to stop it. Its bad practice to interrupt a thread and leave it in an indeterminate state.
dnoyeB at 2007-7-5 22:10:15 > top of Java-index,Core,Core APIs...
# 6
He did as what method he 'could' call in order to halt the execution of a Thread.How to bring a running thread gracefully to a halt is another matter altogether.
andyba at 2007-7-5 22:10:15 > top of Java-index,Core,Core APIs...
# 7
Oh yes, interrupt does not leave the thread in an undetermined state, all the problems related to calling stop() don't happen.
andyba at 2007-7-5 22:10:15 > top of Java-index,Core,Core APIs...
# 8

> Try showing some duke dollars and you will get a

> better response.

Are you assuming that greedy individuals are smarter and more well informed than generous, giving and altruistic people? I guess I wouldn't make that same assumption. Unless you are including Scrooge McDuck whose genius simply cannot be denied!:)

deriderj at 2007-7-5 22:10:15 > top of Java-index,Core,Core APIs...
# 9

> > Try showing some duke dollars and you will get a

> > better response.

>

> Are you assuming that greedy individuals are smarter

> and more well informed than generous, giving and

> altruistic people? I guess I wouldn't make that same

> assumption. Unless you are including Scrooge McDuck

> whose genius simply cannot be denied!:)

Does one have to be greedy to be addicted to useless duke dollars ;)

dnoyeB at 2007-7-5 22:10:15 > top of Java-index,Core,Core APIs...
# 10

> Oh yes, interrupt does not leave the thread in an

> undetermined state, all the problems related to

> calling stop() don't happen.

He said 'terminate' not 'interrupt.' These are 2 differen things.

Interrupting a thread most certainly can leave it in an indeterminate state, if you didn't write the thread.

Calling interrupt is not really going to do anything (if you wrote the thread yourself), unless your thread is actively polling to see if it should be interrupted.

Correct measures have already been posted.

dnoyeB at 2007-7-5 22:10:15 > top of Java-index,Core,Core APIs...
# 11
Also, please note that, if your thread is currently BLOCKED, as for IO, calling interrupt() does not un-block your thread.
javabear at 2007-7-5 22:10:15 > top of Java-index,Core,Core APIs...
# 12
Also, please note that, if your thread is currently BLOCKED, as for IO, calling interrupt() does not un-block your thread.
javabear at 2007-7-5 22:10:15 > top of Java-index,Core,Core APIs...