Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-31-2008, 11:29 PM
Member
 
Join Date: Jul 2008
Posts: 6
rstepler is on a distinguished road
stopping thread...using flags
I have a Gui in which I have animation placed in the thread (Runner). I want the thread to stop when the user pushes the stop button (in a different panel than the animation. Originally I had set up a flag variable in the thread to make it stop until I realized that I would have to create checks for the variable and the animation would continue playing until it reached one of the check points. Is there a way/ another option to make the thread stop quickly. Thank you for any help you offer!

Below is my current code:

Code:
class Runner extends Thread { public SimulationPanel sp; public volatile boolean isRun; public void run() { while (!isRun){ System.out.println("is Run is " + isRun); Boolean isVisible = true; for(int i = 0; i<10; i++){ if (isVisible ==true) { sp.setRepaintValue(SimulationPanel.RepaintType.REMOVERIGHTLINE); isVisible = false; delay(100); }else if (isVisible == false){ sp.setRepaintValue(SimulationPanel.RepaintType.RIGHTLINE); sp.repaint(); delay(100); isVisible = true; } } if(isRun) return; ......} public void stopThread() { System.out.println("stopThread method executing properly"); isRun = true; }
And the Action Listener for the stop button:

Code:
else if (command.equals ("stop")) { new Timer(true).schedule(new TimerTask() { public void run() { System.out.println("Requesting stop"); runnerThread.stopThread();} }, 5); }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-31-2008, 11:36 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
Norm is on a distinguished road
I don't think so, but others may have a better answer.
To stop a thread, exit its run() method. To do it quickly would require testing your flag often.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
data from the main/GUI thread to another runnin thread... cornercuttin Threads and Synchronization 2 04-24-2008 12:30 AM
How to get thread name Java Tip java.lang 0 04-09-2008 08:40 PM
If JNI thread call the java object in another thread, it will crash. skaterxu Advanced Java 0 01-28-2008 09:02 AM
Creating a Thread (extending Java Thread Class) JavaForums Java Blogs 0 12-19-2007 11:31 AM
using Thread one198 New To Java 1 11-21-2007 10:01 AM


All times are GMT +3. The time now is 12:23 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org