Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-28-2009, 09:19 PM
AlejandroPe's Avatar
Member
 
Join Date: Mar 2009
Posts: 37
Rep Power: 0
AlejandroPe is on a distinguished road
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:

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

CODE
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) { }
	}
	}
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 04-28-2009, 09:22 PM
AlejandroPe's Avatar
Member
 
Join Date: Mar 2009
Posts: 37
Rep Power: 0
AlejandroPe is on a distinguished road
Default the entire code
the complete code :

Quote:
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);
}
}
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-28-2009, 10:13 PM
Senior Member
 
Join Date: Aug 2008
Posts: 374
Rep Power: 2
Supamagier is on a distinguished road
Default
You put
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.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 04-28-2009, 10:26 PM
Member
 
Join Date: Mar 2008
Posts: 25
Rep Power: 0
perplexingtrax is on a distinguished road
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:
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 04-29-2009, 04:48 AM
Steve11235's Avatar
Senior Member
 
Join Date: Dec 2008
Posts: 972
Rep Power: 2
Steve11235 is on a distinguished road
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().
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 04-29-2009, 04:05 PM
AlejandroPe's Avatar
Member
 
Join Date: Mar 2009
Posts: 37
Rep Power: 0
AlejandroPe is on a distinguished road
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!!!!!!!!!
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

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

BB 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
How to stop thread from being jumping off the code without executing it..... chiragkini Threads and Synchronization 6 01-22-2009 04:38 AM
Can you stop a gif? xd Exhonour New To Java 0 01-16-2009 09:44 PM
how to stop a thread willemjav Advanced Java 19 09-10-2008 08:11 AM
The safe way to stop a thread Java Tip java.lang 0 04-09-2008 07:31 PM
If JNI thread call the java object in another thread, it will crash. skaterxu Advanced Java 0 01-28-2008 08:02 AM


All times are GMT +2. The time now is 05:53 PM.



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