forked from brianway/java-learning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRun15_join_sleep_2.java
More file actions
61 lines (54 loc) · 1.46 KB
/
Copy pathRun15_join_sleep_2.java
File metadata and controls
61 lines (54 loc) · 1.46 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
50
51
52
53
54
55
56
57
58
59
60
61
package com.brianway.learning.java.multithread.communication.example15;
/**
* Created by Brian on 2016/4/14.
*/
/**
* P187
* join(long)和sleep(long)的区别
* join(long)释放锁
*/
public class Run15_join_sleep_2 {
public static void main(String[] args) {
try {
ThreadB b = new ThreadB();
ThreadA2 a = new ThreadA2(b);
a.start();
Thread.sleep(1000);
ThreadC c = new ThreadC(b);
c.start();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
static class ThreadA2 extends Thread {
private ThreadB b;
public ThreadA2(ThreadB b) {
super();
this.b = b;
}
@Override
public void run() {
try {
synchronized (b) {
b.start();
b.join();
System.out.println("a run before for " + System.currentTimeMillis());
for (int i = 0; i < 100000000; i++) {
Math.random();
}
System.out.println("a run end " + System.currentTimeMillis());
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
/*
输出:
b run begin timer=1460637677744
打印了bService timer=1460637678746
b run end timer=1460637682745
a run before for 1460637682745
a run end 1460637684923
*/