Skip to content

Commit 319de6e

Browse files
committed
[feat]:添加全部停止的方法|两种方式都可以,无论是Thead 还是Executors
1 parent b404b65 commit 319de6e

5 files changed

Lines changed: 153 additions & 11 deletions

File tree

Lines changed: 130 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,150 @@
11
package com.cat.multi.compete;
22

3+
import java.util.*;
4+
import java.util.concurrent.ExecutorService;
5+
import java.util.concurrent.Executors;
6+
import java.util.concurrent.Future;
7+
38
/**
49
* Created by cat on 2018/1/21.
510
* --main--
611
*/
712
public class Main {
813
public static void main(String[] args) {
914

15+
// doTasks();
16+
17+
doTasksOld();
18+
// simpleTest();
19+
}
20+
21+
private static void doTasksOld() {
1022
Repertory<String> stringRepertory = new Repertory<>(800);
1123

1224
PutRunnable<String> putRunnable = new PutRunnable<>(stringRepertory);
1325
TakeRunnable<String> takeRunnable = new TakeRunnable<>(stringRepertory);
14-
15-
new Thread(putRunnable, "put-1").start();
16-
new Thread(putRunnable, "put-2").start();
17-
new Thread(putRunnable, "put-3").start();
26+
ReaderRunnable<String> readerRunnable = new ReaderRunnable<>(stringRepertory);
27+
Thread t1 = new Thread(putRunnable, "put-1");
28+
t1.start();
29+
Thread t2 = new Thread(putRunnable, "put-2");
30+
t2.start();
31+
Thread t3 = new Thread(putRunnable, "put-3");
32+
t3.start();
1833
//
19-
new Thread(takeRunnable, "TAKE-1").start();
20-
new Thread(takeRunnable, "TAKE-2").start();
21-
new Thread(takeRunnable, "TAKE-3").start();
34+
Thread th1 = new Thread(takeRunnable, "TAKE-1");
35+
th1.start();
36+
Thread th2 = new Thread(takeRunnable, "TAKE-2");
37+
th2.start();
38+
Thread th3 = new Thread(takeRunnable, "TAKE-3");
39+
th3.start();
40+
41+
Thread tt1 = new Thread(readerRunnable, "READER-1");
42+
tt1.start();
43+
44+
Timer stopTimer = new Timer();
2245

46+
stopTimer.schedule(new TimerTask() {
47+
@Override
48+
public void run() {
49+
// 停止生产,消费,查看
50+
putRunnable.setStop(true);
51+
takeRunnable.setStop(true);
52+
readerRunnable.setStop(true);
53+
// 关闭全部线程
54+
t1.interrupt();
55+
t2.interrupt();
56+
t3.interrupt();
57+
58+
th1.interrupt();
59+
th2.interrupt();
60+
th3.interrupt();
61+
tt1.interrupt();
62+
63+
stopTimer.cancel();
64+
}
65+
}, 15 * 1000);
66+
}
67+
68+
private static void simpleTest() {
69+
ExecutorService service = Executors.newCachedThreadPool();
70+
Future<?> submit = service.submit(new Runnable() {
71+
@Override
72+
public void run() {
73+
System.out.println("hello world...");
74+
}
75+
});
76+
submit.cancel(true);
77+
service.shutdown();
78+
}
79+
80+
private static void doTasks() {
81+
Repertory<String> stringRepertory = new Repertory<>(800);
2382

83+
PutRunnable<String> putRunnable = new PutRunnable<>(stringRepertory);
84+
TakeRunnable<String> takeRunnable = new TakeRunnable<>(stringRepertory);
2485
ReaderRunnable<String> readerRunnable = new ReaderRunnable<>(stringRepertory);
25-
new Thread(readerRunnable, "READER-1").start();
86+
87+
// 使用线程池替换 new Thread...
88+
System.out.println("main....start");
89+
90+
ExecutorService service1 = Executors.newCachedThreadPool();
91+
Future<?> submit1 = service1.submit(putRunnable); // 启动生产者
92+
93+
ExecutorService service2 = Executors.newCachedThreadPool();
94+
Future<?> submit2 = service2.submit(takeRunnable); // 启动消费者
95+
96+
ExecutorService service3 = Executors.newFixedThreadPool(3);
97+
Future<?> submit3 = service3.submit(readerRunnable); // 启动查看器..(仓管视角~)
2698
System.out.println("main....done");
99+
100+
Timer stopAll = new Timer(); // 工厂倒闭了,不生产了,不销售了,仓管也被开除了~
101+
stopAll.schedule(new TimerTask() {
102+
@Override
103+
public void run() {
104+
System.out.println("全部停止....");
105+
// 停止生产,消费,查看
106+
putRunnable.setStop(true);
107+
takeRunnable.setStop(true);
108+
readerRunnable.setStop(true);
109+
// showLog();
110+
// 关闭线程池
111+
service1.shutdown();
112+
service2.shutdown();
113+
service3.shutdown();
114+
// 关闭未完成的任务 (生产/消费/查看)
115+
submit1.cancel(true); // 需要传 true ,否则可能关不了 ~
116+
submit2.cancel(true);
117+
submit3.cancel(true);
118+
119+
// showThreads();
120+
121+
stopAll.cancel(); // 这个关闭全部的定时器也不需要了
122+
// showThreads();
123+
}
124+
}, 15 * 1000);
125+
}
126+
127+
private static void showLog() {
128+
System.err.println("线程状态-" + Thread.currentThread().getThreadGroup().getName() + " , " +
129+
Thread.currentThread().getThreadGroup().activeCount() + " , " +
130+
Thread.currentThread().getThreadGroup().activeGroupCount() + " ### " +
131+
Thread.currentThread().getName() + " , " + Thread.currentThread().getState().name() + " ..."
132+
);
133+
}
134+
135+
private static void showThreads() {
136+
System.out.println("线程状态-" + Thread.currentThread().getThreadGroup().getName() + " , " +
137+
Thread.currentThread().getThreadGroup().activeCount() + " , " +
138+
Thread.currentThread().getThreadGroup().activeGroupCount() + " ### " +
139+
Thread.currentThread().getName() + " , " + Thread.currentThread().getState().name() + " ..."
140+
);
141+
142+
{
143+
Map<Thread, StackTraceElement[]> traces = Thread.getAllStackTraces();
144+
Set<Thread> threads = traces.keySet();
145+
for (Thread th : threads) {
146+
System.out.println("线程详情--" + th + ":" + Arrays.toString(traces.get(th)));
147+
}
148+
}
27149
}
28150
}

src/com/cat/multi/compete/PutRunnable.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,24 @@ public class PutRunnable<T> implements Runnable {
99

1010
private final Repertory<T> repertory;
1111

12+
private boolean stop = false;
13+
1214
public PutRunnable(Repertory<T> repertory) {
1315
this.x = x;
1416
this.repertory = repertory;
1517
}
1618

19+
public void setStop(boolean stop) {
20+
this.stop = stop;
21+
}
22+
1723
int x = 0;
1824

1925
@Override
2026
public void run() {
2127
String data;
2228
try {
23-
while (true) {
29+
while (!stop) {
2430
if (x % 2 == 0) {
2531
data = "烤鸭 " + x;
2632
} else {

src/com/cat/multi/compete/ReaderRunnable.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,27 @@ public class ReaderRunnable<T> implements Runnable {
1111

1212
private final Repertory<T> repertory;
1313
private final Timer timer;
14+
private boolean stop;
1415

1516
public ReaderRunnable(Repertory<T> repertory) {
1617
this.repertory = repertory;
1718
timer = new Timer();
1819
}
1920

21+
22+
public void setStop(boolean stop) {
23+
this.stop = stop;
24+
}
25+
2026
@Override
2127
public void run() {
2228
timer.schedule(new TimerTask() {
2329
@Override
2430
public void run() {
2531
repertory.show();
32+
if (stop) {
33+
timer.cancel();
34+
}
2635
}
2736
}, 3000, 3000);
2837
}

src/com/cat/multi/compete/Repertory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public void take() throws InterruptedException {
145145
public void show() {
146146
readLock.lock();
147147
try {
148-
System.err.println("SHOW ### " + this.toString());
148+
System.err.println("SHOW ### " + Thread.currentThread().getName()+" " + this.toString());
149149
} finally {
150150
readLock.unlock();
151151
}

src/com/cat/multi/compete/TakeRunnable.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,20 @@
88
public class TakeRunnable<T> implements Runnable {
99

1010
private final Repertory<T> repertory;
11+
private boolean stop;
1112

1213
public TakeRunnable(Repertory<T> repertory) {
1314
this.repertory = repertory;
1415
}
1516

17+
public void setStop(boolean stop) {
18+
this.stop = stop;
19+
}
20+
1621
@Override
1722
public void run() {
1823
try {
19-
while (true) {
24+
while (!stop) {
2025
repertory.take();
2126
}
2227
} catch (InterruptedException e) {

0 commit comments

Comments
 (0)