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

Stopping a thread not with stop

 
shashi nela
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a situation. I want to stop a currently executing thread.

Till java 1.3 it was stip() method which was killing the thread.

But as of java 1.4 this method has been deprecated.

Can u'll please help me to stop the thread without the stop() method.

Any code snippets or some example will be fine.

I have written one example like this but it is not working.

I want ot stop the thread when counter reaches 90.


Thanks in advance,
Shashi
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why dont you make the thread wait indefinitely with wait() ?
 
shashi nela
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No i want to kill the thread.

So that some other process can become active.
 
Henry Wong
author
Sheriff
Posts: 22542
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Considering that you are still in the run() method, why can't you just use "return"?

Henry
 
Campbell Ritchie
Marshal
Posts: 52636
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not certain about this, but I was once told you out to set the thread nullNot sure whether it works.
 
Henry Wong
author
Sheriff
Posts: 22542
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Campbell Ritchie:
Not certain about this, but I was once told you out to set the thread nullNot sure whether it works.


No. It won't work. The system keeps track of all running threads -- so garbage collection of thread objects do not take place, when the thread it represents it still running.

Henry
 
Sol Mayer-Orn
Ranch Hand
Posts: 311
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically, the common recommendation is as follows:

1.
Define some variable or flag, which indicates whether the thread should keep running, or whether it's been asked to stop .
Initialize it to say 'keep runnig'.
Your thread should keep checking this variable, from within its 'run' method. If asked to stop, you can use 'return', or just break any loops you're in.

Note this variable can be anything you want... you can use:
- your own boolean flag where 'true' means 'run' and 'false' means 'stop'
- some object-reference where 'null' means 'stop' and any other value means 'run'
- the 'isInterrupted' flag, that's already built into all threads.

2. Whoever wishes to stop your thread, should set this variable to a value that means 'please stop'.
It's also recommended to call 'interrupt', so that your thread is released from any sleep/wait conditions (otherwise, your thread will only stop when/if it wakes up).


Example (I chose to use the 'isInterrupted' flag as my 'variable') :



Now, you can call 'stopMe()' from the thread itself, like you did in your example:


But IMHO it makes more sense to call it from another thread...
 
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
Combining Sol and Campbell's recommendations, I've seen a lot of code where the control loop checks if a reference to the thread object itself is null, and exits the loop when it becomes so.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by shashi nela:
Till java 1.3 it was stop() method which was killing the thread.

But as of java 1.4 this method has been deprecated.


No, it was deprecated much earlier than that. It must never be used. Neither may the other deprecated methods.

Basically, there is no magic way to stop a thread. You have to work out a way for your threads to co-operate, to achieve the behaviour you want. There are various patterns for this, some of which are mentioned in this thread; but, be careful as some of the stuff in this thread is wrong. Also, try STFW and also searching the archive of this JavaRanch topic.
 
jack catler
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Late addition to this thread....

Based on the example I put together the following servlet thread test..

I was curious and commented out the 'interrupted = true' flag setting as you can see...but...it STILL stops the thread....why is that?

import java.lang.Thread;

public class ConnCheck extends Thread {

private boolean interrupted = false;

private int interval;

public ConnCheck(int interval) {
this.interval = interval;
}

public void run() {

setPriority(Thread.MIN_PRIORITY);

try {
while (true) {
if(isInterrupted()) break;
Thread.sleep(interval);
// ..do stuff....
}
}
catch(java.lang.InterruptedException e) {}
}


// !!! This still stops the thread even with this comment!
synchronized public void stopMe(){
// interrupted = true;
interrupt();
}

synchronized public boolean isInterrupted() {
return interrupted;
}
}


I call the stopMe() function from another servlet which gets a handle to the connCheck servlet from servletContext.

i.e.
ConnCheck d = (ConnCheck)getServletContext().getAttribute("conncheck");
d.stopMe();

It stops it...but why?

Thanks
 
Martin Mathis
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That call to interrupted() sets the interrupt flag to true, so isInterrupted() returns true and breaks out of the loop.
 
This parrot is no more. It has ceased to be. Now it's a tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!