Click to See Complete Forum and Search --> : [RESOLVED] Unable to stop/suspend the thread.
eranga262154
Mar 16th, 2008, 10:52 PM
Hi all,
I have use a Thread just to print some numbers as follows.
Thread tempTH = new Thread(new Runnable() {
public void run(){
int counter = 1;
while(counter != 0){ // Condition to change
try {
System.out.println(counter++);
Thread.sleep(1000);
}
catch (InterruptedException ex){
System.out.println(ex);
}
}
tempTH.start();
}
});
I want to stop this thread, and try to following
tempTH.stop();
Actually I can;'t do that. stop() is not allowed to use. As you know in my IDE simply draw a line across the word stop() and notify that I can't do it.
Why is that, any idea.
ComputerJy
Mar 17th, 2008, 07:09 AM
There is always a work around
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Util {
private static boolean running = true;
public static void main(String[] args) {
try {
Thread countThread = new Thread(new Runnable() {
public void run() {
int counter = 1;
while (counter != 0 && running) {
// Condition to change
try {
System.out.println(counter++);
Thread.sleep(1000);
} catch (InterruptedException ex) {
System.out.println(ex);
}
}
}
});
countThread.start();
System.out.println("Press type anything and press enter to stop counting");
System.in.read();
running = false;
} catch (IOException ex) {
Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
eranga262154
Mar 17th, 2008, 09:06 PM
Actually I have do something else. In every time(actually loop) I used the thread, synchronized it. Then i notify when I want to suspend or stop. It's work actually.
Hope you got that what I say.
Thanks.
eranga262154
Mar 25th, 2008, 03:18 AM
There is always a work around
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Util {
private static boolean running = true;
public static void main(String[] args) {
try {
Thread countThread = new Thread(new Runnable() {
public void run() {
int counter = 1;
while (counter != 0 && running) {
// Condition to change
try {
System.out.println(counter++);
Thread.sleep(1000);
} catch (InterruptedException ex) {
System.out.println(ex);
}
}
}
});
countThread.start();
System.out.println("Press type anything and press enter to stop counting");
System.in.read();
running = false;
} catch (IOException ex) {
Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
I got what you have done here. But I have one question here.
In my code I set the sleep time to 10 seconds (10000 milliseconds actually). If I suspends the thread, it is suspend after 10 seconds. Not suspended at the same time I call to suspend.
Here is my code.
boolean threadSuspend = false;
Thread processThread;
int counter;
public void init(){
counter = 0;
processThread = new Thread(this);
processThread.start();
}
public void run(){
while(true){
try {
System.out.println(counter++);
Thread.sleep(10000);
synchronized(this){
while(threadSuspend){
try{
wait();
}
catch(Exception ex){
Logger.getLogger(Processing.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
catch (InterruptedException ex) {
Logger.getLogger(Processing.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
I've done this to suspend the thread.
synchronized(appProcess){
appProcess.threadSuspend = true;
}
appProcess is the thread class actually.
To resume the thread do following.
synchronized(appProcess){
appProcess.threadSuspend = false;
appProcess.notify();
}
Do you know why this is happened. Actually it suspend and resume the thread at the same if I have done it after 10 seconds. If I try to suspend or resume within the 10 second, it happened after reach to 10 second limit. :(
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.