class ThreadRunMethod implements Runnable { public ThreadRunMethod( String n, int st ) { sleepTime = st; name = n; } public void run( ) { try { Thread self = Thread.currentThread( ); for( int i = 0; i < 10 && !self.isInterrupted( ); i++ ) { System.out.println( self + " " + name + " " + i ); Thread.sleep( sleepTime ); } // ThreadingDemo2.t2.interrupt( ); // kill the slow thread; } catch( InterruptedException e ) { System.out.println( "Terminating.... interrupted while sleeping" ); } } private int sleepTime; private String name; } public class ThreadingDemo2 { public static ThreadRunMethod trm2 = new ThreadRunMethod( "Slow", 35 ); public static Thread t1 = new Thread( new ThreadRunMethod( "FAST", 20 ) ); public static Thread t2 = new Thread( trm2 ); public static Thread t3 = new Thread( new ThreadRunMethod( "SUPER SLOW", 200 ) ); public static void main( String [ ] args ) { System.out.println( "Staring main" ); t1.start( ); t2.start( ); t3.setDaemon( true ); t3.start( ); try { t1.join( ); t2.join( ); } catch( InterruptedException e ) { System.out.println( e ); } System.out.println( "Ending main" ); } }