The Artima Developer Community
Sponsored Link

Java Answers Forum
How to stop a Thread?

3 replies on 1 page. Most recent reply: Jun 23, 2003 2:32 PM by Kishori Sharan

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Venkatesan

Posts: 9
Nickname: venkki
Registered: Jun, 2003

How to stop a Thread? Posted: Jun 19, 2003 11:35 PM
Reply to this message Reply
Advertisement
Inside my main method of java program ,

Thread th=Thread.currentThread();
while(true)
{
here code for calling a method of another class;
{
th.sleep(10000);
}
catch(Exception e)
{
System.out.println("Exception while sleeping thread");
}

I need to know how to know stop this currentThread?


Adam Duffy

Posts: 168
Nickname: adamduffy
Registered: Feb, 2003

Re: How to stop a Thread? Posted: Jun 20, 2003 2:28 AM
Reply to this message Reply
See the following article

http://java.sun.com/j2se/1.4.1/docs/guide/misc/threadPrimitiveDeprecation.html

for suggestions on how to stop threads nicely.

Adam

Namita

Posts: 3
Nickname: nam
Registered: Jun, 2003

Re: How to stop a Thread? Posted: Jun 23, 2003 11:53 AM
Reply to this message Reply
assign the object a null value

-Namita

Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Re: How to stop a Thread? Posted: Jun 23, 2003 2:32 PM
Reply to this message Reply
If I understand your question correctly then
1. You are in main() method of the class you are running
2. You are getting the current thread
3. You are invoking sleep method on current thread
4. After sometime or after some condition is met you want to stop the thread

The currentThread() method inside the main method is giving you the main thread that was started to run your program. If you stop that thread then it is tantamount to stopping the program. If you want to stop the program itself, you can achieve it by calling System.exit() method based on your condition. If you wanted to do something else, please ignore my message.

Flat View: This topic has 3 replies on 1 page
Topic: Xerces: Node Attributes automatically sorted. I need un-ordered how? Previous Topic   Next Topic Topic: The Java Programming Language - To clarify a confusing explanation

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use