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

1    package bookExamples.ch13Threads; 
2     
3    class CounterThread extends Thread { 
4        static int threadCount = 0; 
5        int threadNum; 
6     
7        public CounterThread() { 
8            threadNum = threadCount++; 
9        } 
10    
11       public static void main(String args[]) { 
12           CounterThread ct1 = new CounterThread(); 
13           CounterThread ct2 = new CounterThread(); 
14           CounterThread ct3 = new CounterThread(); 
15           ct1.start(); 
16           ct2.start(); 
17           ct3.start(); 
18       } 
19    
20       public void run() { 
21           // Count from 1 to 10 
22           for (int i = 0; i <= 10; i++) { 
23               System.out.println("Thread " + 
24                       threadNum + " : " + i); 
25               try { 
26                   sleep((long) (Math.random() * 100.0)); 
27               } catch (InterruptedException e) { 
28               } 
29           } 
30       } 
31   }