-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSimpleThread.java
More file actions
49 lines (37 loc) · 1.03 KB
/
SimpleThread.java
File metadata and controls
49 lines (37 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package concurrency;
import java.io.*;
import java.util.concurrent.*;
import net.mindview.util.*;
/**
* RUN:
* javac concurrency/SimpleThread.java && java concurrency.SimpleThread
*
* OUTPUT:
* #2(5), #1(5), #3(5), #4(5), #4(4), #4(3), #2(4), #2(3), #2(2), #4(2), #4(1),
* #5(5), #5(4), #5(3), #5(2), #3(4), #1(4), #1(3), #1(2), #1(1), #3(3), #3(2),
* #3(1), #5(1), #2(1),
*/
public class SimpleThread extends Thread {
private int countDown = 5;
private static int threadCount = 0;
public SimpleThread() {
super(Integer.toString(++threadCount));
start();
}
public String toString() {
return "#" + getName() + "(" + countDown + "), ";
}
public void run() {
while (true) {
System.out.print(this);
if (--countDown == 0) {
return;
}
}
}
public static void main(String[] args) {
for (int i = 0; i< 5; i++) {
new SimpleThread();
}
}
}