//Threads3.java import java.util.*; class Z implements Runnable { //change from X because X.class differs... public void run() { while (true) { System.out.println( "hi from " + Thread.currentThread().getName() ); try { Thread.sleep( (int)(Math.random()*2000) + 2000 ); } catch (InterruptedException e) {} } } } public class Threads3 { public static int JOBS=4; public static int THREADS=12; public static void main( String[] args ) { String[] names = {"gilligan", "skipper", "millionaire", "his wife", "movie star", "professor", "maryann" }; List zList = new ArrayList(); Set threadSet = new HashSet(); Z z1; Thread t1; for (int i=1; i<=JOBS; i++) { z1 = new Z(); //a runnable job (object) zList.add( z1 ); //put reference to it into list } for (int i=1; i<=THREADS; i++) { //assign random job to thread t1 = new Thread( (Z) zList.get( (int)(Math.random()*JOBS) ) ); //give it a random name: //t1.setName( names[(int)(Math.random()*names.length)] ); //dump it into the set of Threads threadSet.add( t1 ); t1.start(); //start it } } }