import java.io.*; import java.util.*; public class ThreadTester { 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("\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; public T1() { start(); } public void run() { while(alive) { System.out.println("Thread 1"); try { sleep(1000); } catch(InterruptedException ie) {} } } public void kill() { alive = false; } } class T2 implements Runnable { private Thread t = new Thread(this); private boolean alive = true; public T2() { t.start(); } public void run() { while(alive) { System.out.println("Thread 2"); try { t.sleep(1000); } catch(InterruptedException ie) {} } } public void kill() { alive = false; } }