Skip to content

Commit a4d02d7

Browse files
committed
update
1 parent b6932bb commit a4d02d7

3 files changed

Lines changed: 75 additions & 61 deletions

File tree

src/main/java/com/code/repository/study/thread/pool/ThreadPoolTest.java

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,69 +5,73 @@
55
public class ThreadPoolTest {
66

77
public static void main(String[] args) {
8-
9-
customThreadPool();
10-
// cachePool();
11-
// fixedThreadPool();
12-
8+
customThreadPool(); // 使用ThreadPoolExecutor 自定义线程池
9+
// cachePool(); // 使用 Executors 创建默认线程池
10+
// fixedThreadPool(); // 使用 Executors 创建默认线程池
1311
}
1412

1513
/**
16-
* 自定义线程池
14+
* 自定义线程池,ThreadPoolExecutor
1715
*/
1816
private static void customThreadPool() {
1917

2018
int corePoolSize = 2; // 主流线程个数
2119
int maximumPoolSize = 4; // 线程最大个数
2220
long keepAliveTime = 0L; // 大于主流线程个数的线程空闲的过期时间 wait for new tasks before terminating
2321
TimeUnit unit = TimeUnit.MILLISECONDS; // 时间单元
24-
BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<Runnable>(); // 工作队列,有三种类SynchronousQueue、LinkedBlockingQueue(在所有 corePoolSize 线程都忙时新任务在队列中等待,maximumPoolSiz失效)、ArrayBlockingQueue,分别对应同步队列、无界队列、有界队列。
25-
26-
ThreadPoolExecutor executorPool = new ThreadPoolExecutor(corePoolSize,
27-
maximumPoolSize, keepAliveTime, unit, workQueue);
28-
29-
for(int i=0;i<20;i++){
30-
System.out.println("start i:"+i);
31-
executorPool.execute(new Runnable(){
22+
BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<Runnable>(1); // 工作队列,有三种类SynchronousQueue、LinkedBlockingQueue(在所有 corePoolSize 线程都忙时新任务在队列中等待)、ArrayBlockingQueue,分别对应同步队列、无界队列(Integer.MAX_VALUE)、有界队列。
23+
ThreadFactory threadFactory = new ThreadFactory(){ // 自定义生成线程的名称
24+
@Override
25+
public Thread newThread(Runnable r) {
26+
Thread myThread=new Thread(r);
27+
myThread.setName("my-"+Thread.currentThread().getName());
28+
return myThread;
29+
}
30+
};
31+
RejectedExecutionHandler handler =new ThreadPoolExecutor.CallerRunsPolicy(); // 队列满,且达到maximumPoolSize时,再添加线程到线程池的拒绝策略,有4种 ThreadPoolExecutor.AbortPolicy抛异常、ThreadPoolExecutor.DiscardPolicy 默默抛弃不处理、ThreadPoolExecutor.DiscardOldestPolicy 抛弃等待最久的、ThreadPoolExecutor.CallerRunsPolicy 由向线程池提交任务的线程来执行该任务
3232

33-
@Override
34-
public void run() {
35-
try {
36-
Thread.sleep(1000);
37-
} catch (InterruptedException e) {
33+
// 构造线程池
34+
ThreadPoolExecutor executorPool = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,threadFactory,handler);
35+
for(int i=1;i<=20;i++){ // 并发最大任务数 = maximumPoolSize+队列深度
36+
try {
37+
int j=i;
38+
executorPool.execute(new Runnable(){
39+
@Override
40+
public void run() {
41+
try {
42+
System.out.println(j + " start at "+Thread.currentThread().getName() );
43+
Thread.sleep(2000);
44+
System.out.println(j +" end at "+Thread.currentThread().getName());
45+
} catch (InterruptedException e) {
46+
}
3847
}
39-
System.out.println(Thread.currentThread().getName()
40-
+ "正在执行。。。");
41-
}
42-
});
43-
System.out.println("end i:"+i);
48+
});
49+
System.out.println(i+" ok");
50+
}catch(Exception e){
51+
System.out.println(i+" fail:"+e.getMessage());
52+
}
4453
}
45-
4654
executorPool.shutdown();
55+
System.out.println("over");
4756
}
4857

4958
/**
50-
* 使用固定数目的线程池<br>
59+
* 使用固定数目的线程池,Executors
5160
* 使用LinkedBlockingQueue固定大小
5261
*/
5362
private static void fixedThreadPool() {
5463
ExecutorService executorPool = Executors.newFixedThreadPool(4);
5564
for (int i = 0; i < 10; i++) {
56-
executorPool.execute(new Runnable() {
57-
58-
@Override
59-
public void run() {
65+
executorPool.execute( () -> {
6066
System.out.println(Thread.currentThread().getName()
6167
+ "正在执行。。。");
62-
}
63-
6468
});
6569
}
6670
executorPool.shutdown();
6771
}
6872

6973
/**
70-
* 动态增减线程数目的线程池<br>
74+
* 动态增减线程数目的线程池,Executors
7175
* 使用SynchronousQueue 实现线程实时增加
7276
*/
7377
private static void cacheThreadPool() {

src/main/java/com/code/repository/study/thread/pool/executor/CachedThreadPool.java

Lines changed: 0 additions & 28 deletions
This file was deleted.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.code.repository.study.thread.synchroniz;
2+
3+
import java.util.concurrent.CountDownLatch;
4+
5+
/**
6+
* CountDownLatch有一个计数器字段,您可以根据需要减少它。然后我们可以用它来阻塞一个调用线程,直到它被计数到零。
7+
* ContDownLatch是一个同步辅助类,在完成某些运算时,只有其他所有线程的运算全部完成,当前运算才继续执行,这就叫闭锁。
8+
*/
9+
public class CloseLockTest implements Runnable{
10+
11+
private CountDownLatch countDownLatch;
12+
13+
public CloseLockTest(CountDownLatch countDownLatch){
14+
this.countDownLatch = countDownLatch;
15+
}
16+
17+
public static void main(String[] args) throws InterruptedException {
18+
CountDownLatch countDownLatch = new CountDownLatch(3);
19+
CloseLockTest ct = new CloseLockTest(countDownLatch);
20+
new Thread(ct).start();
21+
new Thread(ct).start();
22+
new Thread(ct).start();
23+
countDownLatch.await(); // 等待子线程全部执行完
24+
System.out.println("main thread done!");
25+
}
26+
27+
@Override
28+
public void run() {
29+
System.out.println("sub thread run! "+Thread.currentThread().getName());
30+
try {
31+
Thread.sleep(2000L);
32+
} catch (InterruptedException e) {
33+
e.printStackTrace();
34+
}
35+
36+
countDownLatch.countDown();
37+
}
38+
}

0 commit comments

Comments
 (0)