We help IT Professionals succeed at work.

How  to stop a thread ???

rejith_2001
rejith_2001 asked
on
There are some threads running in my server.
I started the thread by calling a function from a servlet.
Is it possible to stop a specified thread (out the many running now in the server) from another servlet using some function.
Comment
Watch Question

Author

Commented:
Somebody please Help

Commented:
Use the suspend method of the thread class to stop the execution of the specified thread.
method:
suspend [thread-id[thread-id]]
This method suspends the or stops the execution of the threads specified or all the nonsystem threads if no thread id's are specified.
You can invoke this function from your specified servlet and pass the ids the threads you are interested to stop.

thanks
ansuman
Hey

U cann't stop a thread in Java2
it is deprecated

But not advised also
Please refer documenatation for more details
Itis allowed in java1.x
Shyam
Commented:
The good way to stop a thread is to make a flag for stoping and inside of Run method to test if this flag is rised go out of the method.  Example:
class MyThred extends thred
{
    public boolean bExit;
    void Run()
    {
      bExit = false;
      // loop of thred or commands
      //and inside of thred loop
      if (bEexit) return;
    }
}

And the way to stop this thred is to set outside of the thred bExit in true !
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
> Use the suspend method of the thread class to stop the execution

Be aware that suspend() only pauses the thread.
Additionally it is unsafe and not recomended to use it.

> suspend [thread-id[thread-id]]

Not sure where this comes from.  SHould be

thread.suspend();

There is no method available to safely stop a thread. The (deprecated) methods stop() and suspend() are considered unsafe and can cause deadlock.

The only way for a thread to safely end is for it's run() method to complete execution. So you need to provide some mechanism to force the run() method to complete. The best way to do this depends on your application.
How about wait() and notify()?
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
wait() (similar to suspend) pauses the thread but does not actually stop it.
HOW TO STOP A THREAD???? Exactly this way, by not returning and continuing the collaboration effort you started.

--->>
This question is LOCKED with a Proposed Answer.  If it helps you, great, accept it and grade it to close.  If not, reject it and comment as to why or what else is needed.

Few additional experts will join this collaboration effort, once a question has been locked.  Just want to confirm this to you.  If more than one expert helps you, you can always split points or award additional help in a new question, within the same topic area.  If you need help from us, post a zero point question here, and include the link:

EXPERT INPUT ON OUTCOME ALWAYS WELCOME.

http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt

Moondancer
Community Support Moderator @ Experts Exchange
All,
I am unlocking this question in preparation for cleanup.  I will return in 7 days to finalize this question.  Please leave any recommendations for the final state of this question, I will take all recommendations into consideration.  Failing any feedback, I may decide in 7 days to delete or PAQ this question with no refund.  Thanks.

SpideyMod
Community Support Moderator @Experts Exchange

Commented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- Points for shekoff

Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
Venabili
EE Cleanup Volunteer

Explore More ContentExplore courses, solutions, and other research materials related to this topic.