Mega Code Archive

 
Categories / Java / Threads
 

Pausing a Thread

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) {           }         }       }     }   } }