Pausing a Thread: set a variable that the thread checks occasionally, call Object.wait()
public class Main {
public static void main(String[] argv) throws Exception {
MyThread thread = new MyThread();
thread.start();
while (true) {
synchronized (thread) {
thread.pause = true;
}
synchronized (thread) {
thread.pause = false;
thread.notify();
}
}
}
}
class MyThread extends Thread {
boolean pause = false;
public void run() {
while (true) {
synchronized (this) {
while (pause) {
try {
wait();
} catch (Exception e) {
}
}
}
}
}
}
Related examples in the same category