-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadPoolMore.java
More file actions
158 lines (133 loc) · 3.88 KB
/
ThreadPoolMore.java
File metadata and controls
158 lines (133 loc) · 3.88 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package shixunTest;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Properties;
import java.util.Scanner;
import java.util.Vector;
public class ThreadPoolMore {
static int num;
public void init(){
//读取属性文件
InputStream in= this.getClass().getResourceAsStream("config.properties");
Properties p=new Properties();
try {
p.load(in);
int num=Integer.parseInt(p.getProperty("num"));
ThreadPoolMore.num=num;
System.out.println("当前模拟线程个数为:"+num);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
ThreadPoolMore test=new ThreadPoolMore();
test.init();
//初始化num大小的线程池
ThreadPool threadPoool=new ThreadPool(ThreadPoolMore.num);
//模拟执行任务
while(true){
System.out.println("请输入任务 名称");
Scanner scanner=new Scanner(System.in);
String taskName=scanner.next();
threadPoool.executeTask(taskName);
}
}
}
class ThreadPool{
private int maxpoolsize;
private Vector<WorkThread> vThreads=new Vector<WorkThread>();
public static ArrayList<String> taskLists=new ArrayList<String>();
//新建maxpoolsize个线程
public ThreadPool(int maxpoolsize){
this.maxpoolsize=maxpoolsize;
for(int i=0;i<maxpoolsize;i++){
WorkThread workThread=new WorkThread();
workThread.start();
vThreads.add(workThread);
}
}
//执行任务
public void executeTask(String taskName){
Boolean couldExecute = false;
//如果任务队列大小<线程池大小,可以执行任务
System.out.println("任务队列 大小:"+taskLists.size()+" "+"线程池大小:"+vThreads.size());
synchronized (couldExecute) {
if(taskLists.size()>=vThreads.size()){
System.out.println("线程池已满,请等待。。。");
try {
Thread.sleep(10*1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//线程池循环查看线程中的空闲线程,如果有空闲线程,将任务分配给它执行
for(int i=0;i<vThreads.capacity();i++){
WorkThread workThread= vThreads.get(i);
//如果当前子线程为空闲线程,将标志位设为true,表示当前前程为工作线程
//在将任务放入任务链表中
if(!workThread.isFlag()){
taskLists.add(taskName);
workThread.setTaskName(taskName);
workThread.setFlag(true);
break;
}
}
}
}
class WorkThread extends Thread{
private boolean flag;
private String taskName;
public String getTaskName() {
return taskName;
}
public void setTaskName(String taskName) {
this.taskName = taskName;
}
public boolean isFlag() {
return flag;
}
public void setFlag(boolean flag) {
this.flag = flag;
if(flag){
//解锁当前线程
synchronized(this){
//优先级--------------------------
this.notify();
}
}
}
public void run() {
//为了不让线程死亡,循环执行
while(true){
if(flag){
System.out.println(Thread.currentThread().getName()+"线程执行当前任务:"+this.taskName);
try {
//模拟执行30秒
Thread.sleep(10*1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(this.taskName+"执行完毕");
//任务执行完毕,将其从任务队列中移除
ThreadPool.taskLists.remove(taskName);
//完成后,将flag对象设置成true
this.setFlag(false);
}else{//flag为false,表示当前线程为空闲线程
try {
System.out.println(Thread.currentThread().getName()+"为非工作线程,正在等待调用");
synchronized (this) {
this.wait();
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}