import java.io.*; import java.util.*; public class ThreadTester2 { public static void main(String[] args) { System.out.println("\nCreating and starting thread 1\n"); T1 t1 = new T1(); try { Thread.sleep(5000); } catch(InterruptedException ie) {} System.out.println("\nCreating and starting thread 2\n"); T2 t2 = new T2(); try { Thread.sleep(5000); } catch(InterruptedException ie) {} System.out.println("\nPausing thread 2\n"); t2.deactivate(); try { Thread.sleep(5000); } catch(InterruptedException ie) {} System.out.println("\nRestarting thread 2\n"); t2.activate(); try { Thread.sleep(5000); } catch(InterruptedException ie) {} System.out.println("\nKilling thread 1\n"); t1.kill(); try { Thread.sleep(5000); } catch(InterruptedException ie) {} System.out.println("\nKilling thread 2\n"); t2.kill(); } } class T1 extends Thread { private boolean alive = true; private boolean active = true; public T1() { start(); } public void run() { while(alive) { while(active) { System.out.println("Thread 1"); try { sleep(1000); } catch(InterruptedException ie) {} } } } public void kill() { active = false; alive = false; } public void activate() { active = true; } public void deactivate() { active = false; } } class T2 implements Runnable { private Thread t = new Thread(this); private boolean alive = true; private boolean active = true; public T2() { t.start(); } public void run() { while(alive) { while(active) { System.out.println("Thread 2"); try { t.sleep(1000); } catch(InterruptedException ie) {} } } } public void kill() { active = false; alive = false; } public void activate() { active = true; } public void deactivate() { active = false; } }