Results 1 to 2 of 2
  1. #1
    rstepler is offline Member
    Join Date
    Jul 2008
    Rep Power

    Default 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:

    Java 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) {
    		isVisible = false;
    			}else if (isVisible == false){
    		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:

    Java Code:
    else if (command.equals ("stop")) {
    			new Timer(true).schedule(new TimerTask() {
    			      public void run() {
    			        System.out.println("Requesting stop");
    			      }, 5);

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Eastern Florida
    Rep Power


    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.

Similar Threads

  1. data from the main/GUI thread to another runnin thread...
    By cornercuttin in forum Threads and Synchronization
    Replies: 2
    Last Post: 04-23-2008, 10:30 PM
  2. How to get thread name
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-09-2008, 06:40 PM
  3. Replies: 0
    Last Post: 01-28-2008, 08:02 AM
  4. using Thread
    By one198 in forum New To Java
    Replies: 1
    Last Post: 11-21-2007, 09:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts