/* Threads yield randomly after printing number */ public class Test4 { final static int NUM_THREADS = 10; public static void main(String[] argv){ MyThread4[] myThreads = new MyThread4[NUM_THREADS]; for (int i=0; i < NUM_THREADS; i++) (myThreads[i] = createThread(i)).start(); for (int i=0; i < NUM_THREADS; i++) try { myThreads[i].join(); } catch (InterruptedException e){ return; } System.out.println(); } public static MyThread4 createThread(int i){ return new MyThread4(new Integer(i).toString()); } } class MyThread4 extends Thread{ final static int NUM_PRINTS = 30; MyThread4(String s){ super(s); } public void run(){ for (int i=0; i < NUM_PRINTS; i++){ System.out.print(getName()); if (Math.random() < 0.5) yield(); } } }