-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTask.java
More file actions
51 lines (41 loc) · 1.3 KB
/
Copy pathTask.java
File metadata and controls
51 lines (41 loc) · 1.3 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
package com.draper.thread;
import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* Created by Draper_HXY 03/06/2018 6:03 PM
* Email: Draper_HXY@163.com
*/
public class Task implements Callable<String>{
private int countDown = 10;
private static int taskCount = 0;
private final int id = taskCount++;
private int sleepTime;
public Task(int sleepTime){
this.sleepTime = sleepTime;
}
public void run() {
try {
System.out.println("#" + id + "开始运行");
System.out.println("#" + id + "休眠" + sleepTime);
Thread.sleep(sleepTime);
System.out.println("#" + id + "结束休眠");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
ExecutorService exec = Executors.newCachedThreadPool();
Random random = new Random(47);
for (int i = 0; i < 5; i++) {
int sleepTime = random.nextInt(10000) + 1000;
// exec.execute(new Task(sleepTime));
}
exec.shutdown();
}
public String call() throws Exception {
Thread.sleep(sleepTime);
return id + "\t" + sleepTime;
}
}