-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadPoolTest.java
More file actions
132 lines (102 loc) · 2.96 KB
/
ThreadPoolTest.java
File metadata and controls
132 lines (102 loc) · 2.96 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
package Chapter14Thread.thread;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;
public class ThreadPoolTest {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.println("输入目录---");
String dictionary=in.nextLine();
System.out.println("输入想要查找的词:--");
String keyword=in.nextLine();
//返回实现ExecutorService接口的ThreadPoolExecutor对象
ExecutorService pool=Executors.newCachedThreadPool();
MathCounter counter=new MathCounter(new File(dictionary), keyword, pool);
Future<Integer> future= pool.submit(counter);
try {
System.out.println("共有"+future.get()+"条数据符合");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//不再提交任务时,调用shutdown结束任务
pool.shutdown();
//最大线程数
int largestPlloSize=((ThreadPoolExecutor)pool).getLargestPoolSize();
System.out.println("最大线程数目:"+largestPlloSize);
}
}
class MathCounter implements Callable<Integer>{
private File dictionary;
private String keyword;
private ExecutorService pool;
private int count;
public MathCounter(File dictionary,String keyword,ExecutorService pool){
this.dictionary=dictionary;
this.keyword=keyword;
this.pool=pool;
}
@Override
public Integer call() throws Exception {
count=0;
try{
File[] files=dictionary.listFiles();
List<Future<Integer>> results=new ArrayList<>();
for(File file:files){
if(file.isDirectory()){
MathCounter counter=new MathCounter(file,keyword,pool);
//线程池提交任务,得到Future对象,用来查询该任务的状态
Future<Integer> result=pool.submit(counter);
results.add(result);
}else{
if(search(file)){
count++;
}
}
//++count
for(Future<Integer> result:results){
try{
count+=result.get();
System.out.println("future里面结果::"+result.get());
}catch(ExecutionException e){
e.printStackTrace();
}
}
}
}catch(InterruptedException e){
e.printStackTrace();
}
return count;
}
//如果包含关键字,返回true
public boolean search(File file){
try{
try(Scanner in=new Scanner(file)){
boolean found=false;
while(in.hasNextLine()&&!found){
String line=in.nextLine();
if(line.contains(keyword)){
found=true;
}
}
return found;
}
}catch (IOException e) {
// TODO Auto-generated catch block
return false;
}
}
}