Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to stop a Thread

Posted on 2001-06-26
10
Medium Priority
?
176 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
Industry Leaders: 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

670 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