Skip to content

Commit 55a26e6

Browse files
add
1 parent 9502999 commit 55a26e6

File tree

4 files changed

+186
-0
lines changed

4 files changed

+186
-0
lines changed

Lock/src/ReadWriteLock.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
/**
22
* 要么多读,要么一写
3+
* <p>
4+
* 注意!
5+
* 读读共享、其他都互斥(写写互斥、读写互斥、写读互斥)
36
*
47
* @Author: zzStar
58
* @Date: 10-09-2020 18:40

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ Java并发编程
6060
不可变性
6161
栈封闭
6262

63+
## 并发容器
64+
ConcurrentHashMap
65+
CopyOnWriteArrayList
66+
并发队列
67+
阻塞队列
68+
非阻塞队列
69+
6370

6471

6572
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package threadpool;
2+
3+
import java.util.concurrent.ExecutorService;
4+
import java.util.concurrent.Executors;
5+
import java.util.concurrent.TimeUnit;
6+
7+
/**
8+
* 关闭线程池
9+
*
10+
* @Author: zzStar
11+
* @Date: 10-12-2020 13:48
12+
*/
13+
public class ShutDown {
14+
public static void main(String[] args) throws InterruptedException {
15+
ExecutorService executorService = Executors.newFixedThreadPool(10);
16+
for (int i = 0; i < 50; i++) {
17+
executorService.execute(new ShutDownTask());
18+
}
19+
Thread.sleep(1000);
20+
// System.out.println("executorService.awaitTermination(3L, TimeUnit.SECONDS) = " + executorService.awaitTermination(3L, TimeUnit.SECONDS));
21+
// System.out.println("executorService.isShutdown() = " + executorService.isShutdown());
22+
// executorService.shutdown();
23+
// System.out.println("executorService.isShutdown() = " + executorService.isShutdown());
24+
// System.out.println("executorService.isTerminated() = " + executorService.isTerminated());
25+
// System.out.println("==========");
26+
// Thread.sleep(2000);
27+
// System.out.println("executorService.isTerminated() = " + executorService.isTerminated());
28+
// executorService.execute(new ShutDownTask());RejectedExecutionException
29+
executorService.shutdownNow();//可得到未执行的线程数组
30+
}
31+
}
32+
33+
class ShutDownTask implements Runnable {
34+
@Override
35+
public void run() {
36+
try {
37+
Thread.sleep(500);
38+
System.out.println(Thread.currentThread().getName());
39+
} catch (InterruptedException e) {
40+
e.printStackTrace();
41+
System.out.println(Thread.currentThread().getName() + "被中断了");
42+
}
43+
}
44+
}
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
package threadpool;
2+
3+
/**
4+
* 线程池分析
5+
*
6+
* @Author: zzStar
7+
* @Date: 10-12-2020 14:07
8+
*/
9+
public class ThreadPoolAnalysis {
10+
11+
/**
12+
* 线程池的组分部分:
13+
* 线程池管理器
14+
* 工作线程
15+
* 任务队列(blocking queue)
16+
* 任务接口(Task)
17+
*/
18+
19+
/**
20+
* Executor <- ExecutorService <- AbstractExecutorService <- ThreadPoolExecutor
21+
*/
22+
23+
/**
24+
* 线程池实现任务复用的原理
25+
* 相同线程执行不同的任务,线程池是让固定的线程不断地从队列中取出任务并执行
26+
*/
27+
28+
/**
29+
* 线程池五种状态
30+
* RUNNING
31+
* SHUTDOWN 不接受新任务,但处理排队任务
32+
* STOP
33+
* TIDYING 所有任务都终止,workerCount为0时,线程会转到TIDYING状态,并将运行terminate()钩子方法
34+
* TERMINATED terminate()运行完成
35+
*/
36+
37+
/**
38+
* 使用线程池注意点:
39+
* 避免任务堆积
40+
* 避免线程数过度增加
41+
* 排查线程泄露(线程回收不了,任务逻辑可能出问题)
42+
*/
43+
}
44+
/*
45+
public void execute(Runnable command) {
46+
if (command == null)
47+
throw new NullPointerException();
48+
*/
49+
/*
50+
* Proceed in 3 steps:
51+
*
52+
* 1. If fewer than corePoolSize threads are running, try to
53+
* start a new thread with the given command as its first
54+
* task. The call to addWorker atomically checks runState and
55+
* workerCount, and so prevents false alarms that would add
56+
* threads when it shouldn't, by returning false.
57+
*
58+
* 2. If a task can be successfully queued, then we still need
59+
* to double-check whether we should have added a thread
60+
* (because existing ones died since last checking) or that
61+
* the pool shut down since entry into this method. So we
62+
* recheck state and if necessary roll back the enqueuing if
63+
* stopped, or start a new thread if there are none.
64+
*
65+
* 3. If we cannot queue task, then we try to add a new
66+
* thread. If it fails, we know we are shut down or saturated
67+
* and so reject the task.
68+
*//*
69+
70+
//ctl记录了线程状态和线程数
71+
int c = ctl.get();
72+
//少于则创建一个线程
73+
if (workerCountOf(c) < corePoolSize) {
74+
//任务command
75+
if (addWorker(command, true))
76+
return;
77+
c = ctl.get();
78+
}
79+
// >=corePoolSize,先放入工作队列中
80+
if (isRunning(c) && workQueue.offer(command)) {
81+
int recheck = ctl.get();
82+
if (! isRunning(recheck) && remove(command))
83+
reject(command);
84+
//防止任务提交却没有线程来执行
85+
else if (workerCountOf(recheck) == 0)
86+
addWorker(null, false);
87+
}
88+
else if (!addWorker(command, false))
89+
reject(command);
90+
}
91+
92+
final void runWorker(Worker w) {
93+
Thread wt = Thread.currentThread();
94+
Runnable task = w.firstTask;
95+
w.firstTask = null;
96+
w.unlock(); // allow interrupts
97+
boolean completedAbruptly = true;
98+
try {
99+
//work不会停止
100+
while (task != null || (task = getTask()) != null) {
101+
w.lock();
102+
// If pool is stopping, ensure thread is interrupted;
103+
// if not, ensure thread is not interrupted. This
104+
// requires a recheck in second case to deal with
105+
// shutdownNow race while clearing interrupt
106+
if ((runStateAtLeast(ctl.get(), STOP) ||
107+
(Thread.interrupted() &&
108+
runStateAtLeast(ctl.get(), STOP))) &&
109+
!wt.isInterrupted())
110+
wt.interrupt();
111+
try {
112+
beforeExecute(wt, task);
113+
try {
114+
task.run();
115+
afterExecute(task, null);
116+
} catch (Throwable ex) {
117+
afterExecute(task, ex);
118+
throw ex;
119+
}
120+
} finally {
121+
task = null;
122+
w.completedTasks++;
123+
w.unlock();
124+
}
125+
}
126+
completedAbruptly = false;
127+
} finally {
128+
processWorkerExit(w, completedAbruptly);
129+
}
130+
}
131+
*/
132+

0 commit comments

Comments
 (0)