//Thread2.java //basic thread //sleep class Y implements Runnable { //change from X because X.class differs... public void run() { System.out.println( "hi from " + Thread.currentThread().getName() + " before my nap" ); try { //Thread class static method sleep(int ms) causes thread not to run //for at least this amount of time. Thread.sleep( (int)(Math.random()*2000) ); } //sleep can throw this checked exception, so try catch is required catch (InterruptedException e) {} //don't do anything... //real reasons for sleeping: 1.slow it down, //2.try to force taking turns //3.execute code periodically System.out.println( "bye from " + Thread.currentThread().getName() + " awake again." ); } } public class Threads2 { public static void main( String[] args ) { Y x1 = new Y(); Thread t2 = new Thread( x1 ); Thread t3 = new Thread( x1 ); Thread t4 = new Thread( x1 ); t2.setName( "gilligan" ); t3.setName( "professor" ); t4.setName( "his wife" ); //they'll sleep for different amounts of time, so wakeup and continuation //order will vary. t2.start(); t3.start(); t4.start(); } }