Solved

How to stop a Thread

Posted on 2001-06-26
10
173 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
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 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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ejb wildfly example 2 113
Problem to Alipay 10 95
spring maven example issues 3 110
How does initial implicit objects  are set up in JSP? 7 41
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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 third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Suggested Courses

752 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