Friday, December 30, 2011

How to stop a running thread

Example for stopping the Running thread in simple java Coding

public class ThreadStop extends Thread {
private boolean running = true;
public void run() {
while (running) {
System.out.print(".");
System.out.flush();
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {}
}
System.out.println("Shutting down thread");
}
public void shutdown() {
running = false;
}
public static void main(String[] args)
throws InterruptedException {
ThreadStop t = new ThreadStop();
t.start();
Thread.sleep(5000);
t.shutdown();
}
}

No comments:

Post a Comment