-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestThreadPool2.java
76 lines (66 loc) · 2.04 KB
/
TestThreadPool2.java
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package demo;
import java.io.Serializable;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class TestThreadPool2
{
private static int produceTaskSleepTime = 2;
private static int produceTaskMaxNumber = 10;
public static void main(String[] args)
{
// 构造一个线程池
ThreadPoolExecutor threadPool = new ThreadPoolExecutor(2, 4, 3, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(3),
new ThreadPoolExecutor.DiscardOldestPolicy());
for (int i = 1; i <= produceTaskMaxNumber; i++)
{
try
{
// 产生一个任务,并将其加入到线程池
String task = "task@ " + i;
System.out.println("put " + task);
threadPool.execute(new ThreadPoolTask(task));
// 便于观察,等待一段时间
Thread.sleep(produceTaskSleepTime);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
}
/**
* 线程池执行的任务
*/
class ThreadPoolTask implements Runnable, Serializable
{
private static final long serialVersionUID = 0;
private static int consumeTaskSleepTime = 2000;
// 保存任务所需要的数据
private Object threadPoolTaskData;
ThreadPoolTask(Object tasks)
{
this.threadPoolTaskData = tasks;
}
public void run()
{
// 处理一个任务,这里的处理方式太简单了,仅仅是一个打印语句
System.out.println(Thread.currentThread().getName());
System.out.println("start .." + threadPoolTaskData);
try
{
// //便于观察,等待一段时间
Thread.sleep(consumeTaskSleepTime);
}
catch (Exception e)
{
e.printStackTrace();
}
threadPoolTaskData = null;
}
public Object getTask()
{
return this.threadPoolTaskData;
}
}