-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDemo_1.java
More file actions
50 lines (46 loc) · 1.46 KB
/
Demo_1.java
File metadata and controls
50 lines (46 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
package com.xycode.executor;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
/**
* ClassName: Demo_1
*
* @Author: xycode
* @Date: 2019/11/29
* @Description: this is description of the Demo_1 class
**/
public class Demo_1 {
public static void main(String[] args) {
ExecutorService es= Executors.newSingleThreadExecutor();
es.submit(()->{
int count=0;
while(true){
if(count>=2) break;
System.out.println(Thread.currentThread().getName()+" started");
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+" done");
++count;
}
});
es.shutdown();
// try {
// es.awaitTermination(100,TimeUnit.SECONDS);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// es.shutdownNow();//对于while(true)的任务,除非自行停止,否则是停止不了的
while(true){
System.out.println(es.isTerminated());
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}