Solved

How to stop a Thread

Posted on 2001-06-26
10
169 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
Comment Utility
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
Comment Utility
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
Comment Utility
Something like the following:

class Blinker
{
   boolean running = false;

   public void stopRunning()
   {
      running = flase;
   }

   public void run()
   {
      running = true;
      while (running)
      {
          // do whatever
      }
   }
}
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
Oops Blinker should implement Runnable :)
0
 

Author Comment

by:HanRui
Comment Utility
When the run() method exits, does that stop the thread from running? Is that the idea? Is the corresponding Thread object then disposed?
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 92

Accepted Solution

by:
objects earned 100 total points
Comment Utility
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
> 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
Comment Utility
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
Comment Utility
Good luck :)
Thanks for the points.
0
 

Author Comment

by:HanRui
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
countHi challenge 25 84
IT Company 5 67
maven project clean install and build and adding as war web plugin 4 35
java set up 1 44
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

728 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now