Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to stop a Thread

Posted on 2001-06-26
10
Medium Priority
?
177 Views
Last Modified: 2010-03-31
The stop() method is deprecated for a Thread, but I can't stop a Thread without using it.

...
    runIt1.stop();
    thisBlinkLoading.thisFrame.dispose();
...

thisBlinkLoading has the run() method with an endless loop that changes the colors of a Label in thisFrame. thisBlinkLoading was the argument to create runIt1=new Thread(thisBlinkLoading);

I want to understand how to trash the Thread, but the only thing that seems to work is to use the stop() method on it first. There must be another way if stop() is deprecated, but I can't figure it out.

This is just an exercise. The code has no value other than that.
0
Comment
Question by:HanRui
  • 6
  • 4
10 Comments
 
LVL 92

Expert Comment

by:objects
ID: 6229629
What you want to do is provide a mechanism to notify your thread that it should finish.
If you can post your run method I can help determine the best way to do this.
0
 

Author Comment

by:HanRui
ID: 6229642
I use the below for teaching. There is a MediaTracker object in the init() method of an Applet that waits until an image has been loaded. After the waitForID() method reports, that's when I want to stop the loop below. I can stop the loop with a static boolean arg in while(arg), but I would like to understand the question posed about deprecation of the stop() method. The stop() method will halt the runIt1 Thread, but it is deprecated.




public void run(){

/* Loop endlessly until this class instance is disposed. */

while(true)
 {

/* Print the name of the current thread
 */

System.out.println(
  "Current Thread is " + Thread.currentThread().getName());

try{

/* The sleep() method is a static method within Thread.class.
 *     The Thread class is found in java.lang.Thread.
 *     The term "static" means "only one copy of the method
 *     will exist in all instances of the Thread object."
 *     To reference a static method, use the class name
 *     followed by the method, as used below.
 */
        Thread.sleep(1000);
        loading.setForeground(Color.black);
        }
        catch (InterruptedException e) {}

/* Notice that we did not import java.lang.Thread in
 * this class. The Java Language Specification, 2nd Edition states:
 *
 * 7.5.3 Automatic Imports
 * Each compilation unit automatically imports all of the public type names  
 * declared in the predefined package java.lang, as if the declaration:
 *
 *                   import java.lang.*;
 *
 * appeared at the beginning of each compilation unit, immediately following
 * any package statement.
 */

try{
        Thread.sleep(1000);
        loading.setForeground(Color.green);
        }
        catch (InterruptedException e) {}
 }

/* end of run() method */
}
0
 
LVL 92

Expert Comment

by:objects
ID: 6229647
Something like the following:

class Blinker
{
   boolean running = false;

   public void stopRunning()
   {
      running = flase;
   }

   public void run()
   {
      running = true;
      while (running)
      {
          // do whatever
      }
   }
}
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Expert Comment

by:objects
ID: 6229651
Oops Blinker should implement Runnable :)
0
 

Author Comment

by:HanRui
ID: 6229654
When the run() method exits, does that stop the thread from running? Is that the idea? Is the corresponding Thread object then disposed?
0
 
LVL 92

Accepted Solution

by:
objects earned 400 total points
ID: 6229655
0
 
LVL 92

Expert Comment

by:objects
ID: 6229658
> When the run() method exits, does that stop the thread
> from running?

Yes

> Is that the idea? Is the corresponding
> Thread object then disposed?

Exactly (though I don't think the actual Thread object will become available for gc until their are no references to it).
0
 

Author Comment

by:HanRui
ID: 6229663
Ok. I'll try the technique of making run() fall through to exit, if I understand it correctly.
0
 
LVL 92

Expert Comment

by:objects
ID: 6229671
Good luck :)
Thanks for the points.
0
 

Author Comment

by:HanRui
ID: 6229763
It worked! I used a static method definition of stopRunning() so it could be called from the init() method of the Applet after the images were loaded.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question