/** * This code is edited from the book: * * Winder, R and Roberts, G (1998) * Developing Java Software * John Wiley & Sons. * * It is copyright (c) 1997 Russel Winder * and Graham Roberts. */ class Calculate extends Thread{ private int id ; public Calculate(int i){ id = i ; } // Do some sort of calculation public void run(){ System.out.println("Calculation " + id + "started") ; int x = 2 ; for (int n = 0 ; n < 1000000 ; n++){ x = x * 2 / 2 ; } System.out.println("Calculation " + id + "done") ; } } class ThreadTest4{ public static void main(String[] args){ // Create an array of threads and start each // one. Thread t[] = new Thread[10] ; for (int i = 0 ; i < 10 ; i++){ t[i] = new Calculate(i) ; t[i].start() ; } // Join to each thread in order, so that the // main program thread does not terminate // until all the threads have finished. for (int i = 0 ; i < 10 ; i++){ try{ t[i].join() ; System.out.println("Calculate " + i + " joined") ; } catch (InterruptedException e){ } } } }