Solved

How to stop a Thread

Posted on 2001-06-26
10
174 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 100 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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.
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 about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses

628 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