Results 1 to 6 of 6
  1. #1
    AlejandroPe's Avatar
    AlejandroPe is offline Member
    Join Date
    Mar 2009
    Posts
    37
    Rep Power
    0

    Question [SOLVED] How to stop a thread

    Hello everyone!
    I tried to run an example code from my book but I came across first with a deprecated stop() and now, after changing stop() for a "better" solution, I'm getting this:

    DigitalThreads.java:23: <identifier> expected
    runner = null;
    ^
    1 error
    The actual question is:
    Where to put runner = null;??

    CODE
    Java Code:
    import java.awt.*;
    import java.util.Date;
    import java.awt.Font;
    
    public class DigitalThreads extends java.applet.Applet implements Runnable {
       Font theFont = new Font("TimesRoman", Font.BOLD, 24);
       Date theDate;
       Thread runner;
    	
    	public void start() {
    		if (runner == null) {
    			runner = new Thread(this);
    			runner.start();
    		}
    	}
    
    	//public void stop() {
    	//	if (runner != null) {
    	//		runner.stop();
    	//		runner = null;
    	//	}
    	// }
    runner = null;	
    
    public void run() {
    	Thread thisThread = Thread.currentThread();
    	while (runner == thisThread) {
    	while(true) {
    		theDate = new Date();
    		repaint();
    		try {Thread.sleep(1000); }
    		catch (InterruptedException e) { }
    	}
    	}

  2. #2
    AlejandroPe's Avatar
    AlejandroPe is offline Member
    Join Date
    Mar 2009
    Posts
    37
    Rep Power
    0

    Default the entire code

    the complete code

    import java.awt.*;
    import java.util.Date;
    import java.awt.Font;

    public class DigitalThreads extends java.applet.Applet implements Runnable {
    Font theFont = new Font("TimesRoman", Font.BOLD, 24);
    Date theDate;
    Thread runner;

    public void start() {
    if (runner == null) {
    runner = new Thread(this);
    runner.start();
    }
    }

    //public void stop() {
    // if (runner != null) {
    // runner.stop();
    // runner = null;
    // }
    // }
    runner = null;

    public void run() {
    Thread thisThread = Thread.currentThread();
    while (runner == thisThread) {
    while(true) {
    theDate = new Date();
    repaint();
    try {Thread.sleep(1000); }
    catch (InterruptedException e) { }
    }
    }
    }
    public void paint(Graphics g) {
    g.setColor(Color.red);
    g.setFont(theFont);
    g.drawString(theDate.toString(), 10, 50);
    }
    }

  3. #3
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    You put
    Java Code:
    runner = null;
    outside a method, that can't. You should put it inside a method. For example, the stop method you commented out ;)
    I die a little on the inside...
    Every time I get shot.

  4. #4
    Join Date
    Mar 2008
    Posts
    25
    Rep Power
    0

    Default

    im presuming you want to start the thread when the applet loads, in that case just set the 'Thread runner = null;' when its declared ie:
    Java Code:
    public class DigitalThreads extends java.applet.Applet implements Runnable {
    Font theFont = new Font("TimesRoman", Font.BOLD, 24);
    Date theDate;
    Thread runner = null;
    if you wanted to do a timer, just set the value of the thread when the user clicks and set the value to the condtion of the loop to stop it.

  5. #5
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    OK. You need to do several things.

    1. Once you start runner, leave the runner variable alone until after it is done.

    2, Get rid of the thisThread logic in run().

    3. Create a new instance variable called "keepGoing", like this:

    private volatile boolean keepGoing = false;

    4. At the beginning of the run() method, set keepGoing = true.

    5. You need one while loop:

    while (keepGoing && !Thread.currentThread.isInterupted()) {
    ...
    }

    6. In your catch block, add the line

    Thread.currentThread.interrupt();

    7. When you want your thread to stop, set keepGoing = false;

    The "volatile" keyword ensures that keepGoing holds the same value for both threads. The JVM is *allowed* (not required) to make copes of all variables for each thread. You have to use "synchronize" to update all of them, or you can just mark some of them volatile. In this case, volatile should be more efficient.

    Using a flag to end a thread is generally more gentle than interrupt(), which can cause Exception's to be thrown and break things like streams and sockets.

    It is a good idea to check if the thread was interrupted. You *cannot* assume you will get an Exception, since Exceptions are thrown only when the thread is blocking. If you do get an Exception, interrupt() the thread again to set its interrupted flag. It won't cause an Exception because the thread is active.

    I also noticed that your thread is doing some painting. THAT IS A MAJOR NO-NO! All UI updates *must* be performed on the event dispatcher thread. You will get unpredictable results! Use a thread to prepare the UI update, but do the update on the EDT.

    Google EventQueue.invokeLater().

  6. #6
    AlejandroPe's Avatar
    AlejandroPe is offline Member
    Join Date
    Mar 2009
    Posts
    37
    Rep Power
    0

    Thumbs up it works!!

    It works! I just needed to assign runner the value null when I declare it along with the other variables at the beginning, just like Perplex advised.

    Thank you everyone!!!!!!!!!

Similar Threads

  1. How to stop thread from being jumping off the code without executing it.....
    By chiragkini in forum Threads and Synchronization
    Replies: 6
    Last Post: 01-22-2009, 04:38 AM
  2. Can you stop a gif? xd
    By Exhonour in forum New To Java
    Replies: 0
    Last Post: 01-16-2009, 09:44 PM
  3. how to stop a thread
    By willemjav in forum Advanced Java
    Replies: 19
    Last Post: 09-10-2008, 08:11 AM
  4. The safe way to stop a thread
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-09-2008, 07:31 PM
  5. Replies: 0
    Last Post: 01-28-2008, 08:02 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
  •