-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
152 lines (126 loc) · 5.34 KB
/
Main.java
File metadata and controls
152 lines (126 loc) · 5.34 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
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package com.cat.multi.compete;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
/**
* Created by cat on 2018/1/21.
* --main--
*/
public class Main {
public static void main(String[] args) {
doTasks();
// doTasksOld();
// simpleTest();
}
private static void doTasksOld() {
Repertory<String> stringRepertory = new Repertory<>(800);
PutRunnable<String> putRunnable = new PutRunnable<>(stringRepertory);
TakeRunnable<String> takeRunnable = new TakeRunnable<>(stringRepertory);
ReaderRunnable<String> readerRunnable = new ReaderRunnable<>(stringRepertory);
Thread t1 = new Thread(putRunnable, "put-1");
t1.start();
Thread t2 = new Thread(putRunnable, "put-2");
t2.start();
Thread t3 = new Thread(putRunnable, "put-3");
t3.start();
//
Thread th1 = new Thread(takeRunnable, "TAKE-1");
th1.start();
Thread th2 = new Thread(takeRunnable, "TAKE-2");
th2.start();
Thread th3 = new Thread(takeRunnable, "TAKE-3");
th3.start();
Thread tt1 = new Thread(readerRunnable, "READER-1");
tt1.start();
Timer stopTimer = new Timer();
stopTimer.schedule(new TimerTask() {
@Override
public void run() {
// 停止生产,消费,查看
putRunnable.setStop(true);
takeRunnable.setStop(true);
readerRunnable.setStop(true);
// 关闭全部线程
t1.interrupt();
t2.interrupt();
t3.interrupt();
th1.interrupt();
th2.interrupt();
th3.interrupt();
tt1.interrupt();
stopTimer.cancel();
}
}, 15 * 1000);
}
private static void simpleTest() {
ExecutorService service = Executors.newCachedThreadPool();
Future<?> submit = service.submit(new Runnable() {
@Override
public void run() {
System.out.println("hello world...");
}
});
submit.cancel(true);
service.shutdown();
}
private static void doTasks() {
Repertory<String> stringRepertory = new Repertory<>(800);
PutRunnable<String> putRunnable = new PutRunnable<>(stringRepertory);
TakeRunnable<String> takeRunnable = new TakeRunnable<>(stringRepertory);
ReaderRunnable<String> readerRunnable = new ReaderRunnable<>(stringRepertory);
// 使用线程池替换 new Thread...
System.out.println("main....start");
ExecutorService service1 = Executors.newCachedThreadPool();
Future<?> submit1 = service1.submit(putRunnable); // 启动生产者
ExecutorService service2 = Executors.newCachedThreadPool();
Future<?> submit2 = service2.submit(takeRunnable); // 启动消费者
ExecutorService service3 = Executors.newFixedThreadPool(3);
Future<?> submit3 = service3.submit(readerRunnable); // 启动查看器..(仓管视角~)
System.out.println("main....done");
Timer stopAll = new Timer(); // 工厂倒闭了,不生产了,不销售了,仓管也被开除了~
stopAll.schedule(new TimerTask() {
@Override
public void run() {
System.out.println("全部停止....");
// 停止生产,消费,查看
putRunnable.setStop(true);
takeRunnable.setStop(true);
readerRunnable.setStop(true);
// showLog();
// 关闭线程池
service1.shutdown();
service2.shutdown();
service3.shutdown();
// 关闭未完成的任务 (生产/消费/查看)
submit1.cancel(true); // 需要传 true ,否则可能关不了 ~
submit2.cancel(true);
submit3.cancel(true);
// showThreads();
stopAll.cancel(); // 这个关闭全部的定时器也不需要了
showThreads();
}
}, 15 * 1000);
}
private static void showLog() {
System.err.println("线程状态-" + Thread.currentThread().getThreadGroup().getName() + " , " +
Thread.currentThread().getThreadGroup().activeCount() + " , " +
Thread.currentThread().getThreadGroup().activeGroupCount() + " ### " +
Thread.currentThread().getName() + " , " + Thread.currentThread().getState().name() + " ..."
);
}
private static void showThreads() {
System.out.println("线程状态-" + Thread.currentThread().getThreadGroup().getName() + " , " +
Thread.currentThread().getThreadGroup().activeCount() + " , " +
Thread.currentThread().getThreadGroup().activeGroupCount() + " ### " +
Thread.currentThread().getName() + " , " + Thread.currentThread().getState().name() + " ..."
);
{
Map<Thread, StackTraceElement[]> traces = Thread.getAllStackTraces();
Set<Thread> threads = traces.keySet();
for (Thread th : threads) {
System.out.println("线程详情--" + th + ":" + Arrays.toString(traces.get(th)));
}
}
}
}