Skip to content

Commit f825603

Browse files
author
cicadasmile
committed
Java并发:Executor线程池框架简介
1 parent 5f95987 commit f825603

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.multy.thread.block08executor;
2+
import java.util.concurrent.*;
3+
4+
public class Executor01 {
5+
// 定义线程池
6+
private static ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(
7+
3,10,5000,TimeUnit.SECONDS,
8+
new SynchronousQueue<>(),Executors.defaultThreadFactory(),new ExeHandler());
9+
public static void main(String[] args) {
10+
for (int i = 0 ; i < 100 ; i++){
11+
poolExecutor.execute(new PoolTask(i));
12+
//带返回值:poolExecutor.submit(new PoolTask(i));
13+
}
14+
}
15+
}
16+
// 定义线程池任务
17+
class PoolTask implements Runnable {
18+
19+
private int numParam;
20+
21+
public PoolTask (int numParam) {
22+
this.numParam = numParam;
23+
}
24+
@Override
25+
public void run() {
26+
try {
27+
System.out.println("PoolTask "+ numParam+" begin...");
28+
Thread.sleep(5000);
29+
} catch (Exception e) {
30+
e.printStackTrace();
31+
}
32+
}
33+
public int getNumParam() {
34+
return numParam;
35+
}
36+
public void setNumParam(int numParam) {
37+
this.numParam = numParam;
38+
}
39+
}
40+
// 定义异常处理
41+
class ExeHandler implements RejectedExecutionHandler {
42+
@Override
43+
public void rejectedExecution(Runnable runnable, ThreadPoolExecutor executor) {
44+
System.out.println("ExeHandler "+executor.getCorePoolSize());
45+
executor.shutdown();
46+
}
47+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.multy.thread.block08executor;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.concurrent.Callable;
6+
import java.util.concurrent.ExecutorService;
7+
import java.util.concurrent.Executors;
8+
import java.util.concurrent.Future;
9+
10+
public class Executor02 {
11+
12+
public static void main(String[] args) {
13+
// 初始化校验任务
14+
List<CheckTask> checkTaskList = new ArrayList<>() ;
15+
initList(checkTaskList);
16+
// 定义线程池
17+
ExecutorService executorService ;
18+
if (checkTaskList.size() < 10){
19+
executorService = Executors.newFixedThreadPool(checkTaskList.size());
20+
}else{
21+
executorService = Executors.newFixedThreadPool(10);
22+
}
23+
// 批量处理
24+
List<Future<Boolean>> results = new ArrayList<>() ;
25+
try {
26+
results = executorService.invokeAll(checkTaskList);
27+
} catch (InterruptedException e) {
28+
Thread.currentThread().interrupt();
29+
}
30+
// 查看结果
31+
for (Future<Boolean> result : results){
32+
try {
33+
System.out.println(result.get());
34+
// System.out.println(result.get(10000,TimeUnit.SECONDS));
35+
} catch (Exception e) {
36+
e.printStackTrace() ;
37+
}
38+
}
39+
// 关闭线程池
40+
executorService.shutdownNow();
41+
}
42+
43+
private static void initList (List<CheckTask> checkTaskList){
44+
checkTaskList.add(new CheckTask("root","123")) ;
45+
checkTaskList.add(new CheckTask("root1","1234")) ;
46+
checkTaskList.add(new CheckTask("root2","1235")) ;
47+
}
48+
}
49+
// 校验任务
50+
class CheckTask implements Callable<Boolean> {
51+
private String userName ;
52+
private String passWord ;
53+
public CheckTask(String userName, String passWord) {
54+
this.userName = userName;
55+
this.passWord = passWord;
56+
}
57+
@Override
58+
public Boolean call() throws Exception {
59+
// 校验账户+密码
60+
if (userName.equals("root") && passWord.equals("123")){
61+
return Boolean.TRUE ;
62+
}
63+
return Boolean.FALSE ;
64+
}
65+
}

0 commit comments

Comments
 (0)