Skip to content

Commit 47a87a0

Browse files
committed
add > juc TimeUnit test code
1 parent 58f222b commit 47a87a0

1 file changed

Lines changed: 43 additions & 0 deletions

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package juc;
2+
3+
import java.util.concurrent.TimeUnit;
4+
5+
/**
6+
* Created by 李恒名 on 2017/6/17.
7+
*/
8+
public class TimeUnitTest {
9+
10+
public static void main(String[] args) throws InterruptedException {
11+
//睡眠13分钟
12+
TimeUnit.MINUTES.sleep(13);
13+
//Thread.sleep(780000);//这样写你知道是多久吗?
14+
//Thread.sleep(13*60*1000);//这样写会稍微好些
15+
16+
//睡眠1小时
17+
TimeUnit.HOURS.sleep(1);
18+
//Thread.sleep(3600000);
19+
20+
TimeUnitTest test = new TimeUnitTest();
21+
22+
Thread thread = new Thread(() -> test.work());
23+
24+
//10秒内Join
25+
TimeUnit.SECONDS.timedJoin(thread,10);
26+
//thread.join(10000);
27+
28+
29+
}
30+
31+
public synchronized void work() {
32+
System.out.println("Begin Work");
33+
try {
34+
//等待30秒后,自动唤醒继续执行
35+
TimeUnit.SECONDS.timedWait(this, 30);
36+
//wait(30000);
37+
} catch (InterruptedException e) {
38+
e.printStackTrace();
39+
}
40+
System.out.println("Work End");
41+
}
42+
43+
}

0 commit comments

Comments
 (0)