-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreadSamp.java
More file actions
53 lines (39 loc) · 1008 Bytes
/
threadSamp.java
File metadata and controls
53 lines (39 loc) · 1008 Bytes
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
50
51
52
53
public class threadSamp {
/**
* @param args
*/
private static int i = 0;
public static void main(String[] args) throws Exception {
System.out.println("Process1 begin");
for (int k = 0; k < 5; k++) {
Thread thread = new innerThread2();
thread.start();
}
Thread.currentThread().sleep(5000);
System.out.println("Process1 end");
System.out.println("Process2 begin");
for (int k = 0; k < 5; k++) {
Thread thread = new Thread(new innerThread());
thread.start();
}
Thread.currentThread().sleep(5000);
System.out.println("Process2 end");
}
static class innerThread implements Runnable {
private final int threadId = i++;
public void run() {
for (int j=0; j<5; j++) {
System.out.println("#" + threadId + ": " + j);
}
}
}
static class innerThread2 extends Thread {
private final int threadId = i++;
@Override
public void run() {
for (int j=0; j<5; j++) {
System.out.println("#" + threadId + ": " + j);
}
}
}
}