/Users/lyon/j4p/src/bookExamples/ch13Threads/ThreadTest.java

1    package bookExamples.ch13Threads; 
2     
3    public class ThreadTest { 
4     
5        public static void main(String args[]) { 
6            int numberOfThreads = 5; 
7            Thread t[] = new Thread[numberOfThreads]; 
8            System.out.println("Beginning thread test:"); 
9            for (int i = 0; i < numberOfThreads - 1; i++) { 
10               t[i] = new Thread(new Hello(i)); 
11               t[i].start(); 
12           } 
13       } 
14   } 
15    
16    
17   class Hello implements Runnable { 
18       int i = 0; 
19       int numberOfTimesRun = 0; 
20    
21       Hello(int id) { 
22           i = id; 
23       } 
24    
25       public void run() { 
26           for (int j = 0; j < 10; j++) { 
27               System.out.println( 
28                       "Hello #" + i + 
29                       " numberOfTimesRun=" + numberOfTimesRun++); 
30               try { 
31                   Thread.sleep( 
32                           (int) (Math.random() * 1000)); 
33               } // try 
34               catch (InterruptedException e) { 
35                   e.printStackTrace(); 
36               } 
37           } // for 
38           System.out.println("Hello #" + i + " is done!"); 
39       } 
40   } 
41