forked from lihengming/java-codes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimeUnitTest.java
More file actions
43 lines (32 loc) · 1.04 KB
/
Copy pathTimeUnitTest.java
File metadata and controls
43 lines (32 loc) · 1.04 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
package juc;
import java.util.concurrent.TimeUnit;
/**
* Created by 李恒名 on 2017/6/17.
*/
public class TimeUnitTest {
public static void main(String[] args) throws InterruptedException {
//睡眠13分钟
TimeUnit.MINUTES.sleep(13);
//Thread.sleep(780000);//这样写你知道是多久吗?
//Thread.sleep(13*60*1000);//这样写会稍微好些
//睡眠1小时
TimeUnit.HOURS.sleep(1);
//Thread.sleep(3600000);
TimeUnitTest test = new TimeUnitTest();
Thread thread = new Thread(() -> test.work());
//10秒内Join
TimeUnit.SECONDS.timedJoin(thread,10);
//thread.join(10000);
}
public synchronized void work() {
System.out.println("Begin Work");
try {
//等待30秒后,自动唤醒继续执行
TimeUnit.SECONDS.timedWait(this, 30);
//wait(30000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Work End");
}
}