Results 1 to 2 of 2
  1. #1
    apeto is offline Member
    Join Date
    May 2012
    Posts
    1
    Rep Power
    0

    Default Thread wont stop

    Hi, I was programming an Applet and I realize that one thread that I started to perform all the applet
    job, was not stopping when running flag was set to false. I'm not using stop() as is was depricated
    so I have a 'running' flag that the thread chack on every loop to exit the run() method.

    Here is the code and the Applet never comes out of the join() call.

    But....
    If I just app a simple line like System.out.println(running), then it works as it is supous to and the thread stops
    and the join() coms back.
    Alsow if in debug mode, then it works fine even without the System.out.printl() line.

    I thought it was some problem with the Applet JVM of my browser but
    I tried to run it as a normal app with its static mail( argv ) method and I get the same result.

    Any suggestions ?
    Thanks
    Albert

    Here is the code.

    Java Code:
    package net.apeto.applet;
    
    import java.applet.Applet;
    import java.awt.BorderLayout;
    
    /**
     *
     * @author Albert Bonomo 
     */
    public class Error extends Applet implements Runnable {
        Thread gameThread;                            // Thread which runs the main game loop
        boolean running = true;                      // is the game loop running
        private int i = 0;
        
        public void run() {
            running = true;
            System.out.println("Thread Start");
            while(running) {
                i++;
                //System.out.println("running " + i);      // uncommnet and it works fine !!!
                
            }
            System.out.println("Thread Stoped");
        }
        
        public void init() {
            System.out.println("Init");
            setLayout(new BorderLayout());                
        }
        
        public void start() {
            System.out.println("Start");
            
            // Init Thread
            gameThread = new Thread(this);
            gameThread.start();
        }
        
        public void stop() {
            System.out.println("Stop");
            running = false; 
            System.out.println("running = " + running);
            try {
                System.out.println("in join");
                gameThread.join();
                System.out.println("out join");
            } 
            catch (InterruptedException e) {
                e.printStackTrace();
            }
            
        }
        
        public void destroy() {
            System.out.println("Destroy");
            super.destroy();
            System.out.println("Clear up");
        }
        
        public static void main(String[] args) { 
            Error error = new Error();
            error.init();
            error.start();
            System.out.println("Sleep 5 sec");
            try { Thread.sleep(5000); } catch ( Exception e){}
            System.out.println("Back from Sleep");
            error.stop();
            error.destroy();
            System.exit(0);
        }
        
    }
    Last edited by JosAH; 05-25-2012 at 07:34 PM. Reason: added [code] ... [/code] tags

  2. #2
    doWhile is online now Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Thread wont stop

    Works for me...try marking your flag variable (eg running) as volatile

Similar Threads

  1. Stop Thread
    By TyCox94 in forum New To Java
    Replies: 31
    Last Post: 09-30-2011, 05:38 AM
  2. help!, while loop wont stop!
    By crank6015 in forum New To Java
    Replies: 4
    Last Post: 03-24-2011, 03:27 AM
  3. Thread won't stop
    By bubbless in forum Threads and Synchronization
    Replies: 15
    Last Post: 10-20-2009, 11:58 PM
  4. Help to stop a thread
    By raghu_lzybns in forum New To Java
    Replies: 4
    Last Post: 07-09-2009, 05:39 PM
  5. how to stop a thread
    By willemjav in forum Advanced Java
    Replies: 19
    Last Post: 09-10-2008, 08:11 AM

Tags for this Thread

Posting Permissions

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