/Users/lyon/j4p/src/bookExamples/ch13Threads/ThreadExample.java
|
1 package bookExamples.ch13Threads;
2
3 public class ThreadExample {
4 static int numberOfInstances = 0;
5
6 class HelloWorldExample implements Runnable {
7 int id = 0;
8
9 HelloWorldExample() {
10 id = numberOfInstances;
11 numberOfInstances++;
12 }
13
14 public void run() {
15 System.out.println("time to sleep! Id=" + id);
16 try {
17 Thread.sleep(10 * 1000);
18 } catch (InterruptedException e) {
19 }
20 System.out.println("hello I am awake! id=" + id);
21 }
22 }
23
24 HelloWorldExample hwe = new HelloWorldExample();
25 Thread t = new Thread(hwe);
26
27 ThreadExample() {
28 t.start();
29 }
30
31 public static void main(String args[]) {
32 ThreadExample te[] = new ThreadExample[5000];
33 for (int i = 0; i < te.length; i++) {
34 te[i] = new ThreadExample();
35 }
36 }
37 }
38
39