• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Stopping a thread not with stop

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No i want to kill the thread.

So that some other process can become active.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Considering that you are still in the run() method, why can't you just use "return"?

Henry
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 311
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic