Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

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: 23919
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: 75676
354
  • 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: 23919
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: 24204
44
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
  • 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.
 
And then we all jump out and yell "surprise! we got you this tiny ad!"
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic