forked from damaohongtu/JavaInterview
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathThreadPoolExecutorTest.java
More file actions
47 lines (37 loc) · 1.24 KB
/
ThreadPoolExecutorTest.java
File metadata and controls
47 lines (37 loc) · 1.24 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
package Java8Test;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;
/**
* @Classname ThreadPoolExecutorTest
* @Description TODO
* @Date 19-7-13 下午4:15
* @Created by mao<tianmao818@qq.com>
*/
public class ThreadPoolExecutorTest {
public static void main(String[] args) {
ThreadPoolExecutor executor = new ThreadPoolExecutor(3, 5, 100, TimeUnit.MILLISECONDS,
new ArrayBlockingQueue<Runnable>(5));
for (int i = 0; i < 10; i++) {
executor.execute(new MyTask(i));
System.out.println("线程池中线程数:"+executor.getPoolSize()+",队列中等待执行的任务数:"+
executor.getQueue().size()+",已执行完的任务数:"+executor.getCompletedTaskCount());
}
}
}
class MyTask implements Runnable {
private int id;
public MyTask(int id) {
this.id = id;
}
@Override
public void run() {
System.out.println("开始执行:任务 " + id);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("执行完毕:任务 " + id);
}
}