package org.xml.upload;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class SaveReviewThreadPool {
private ArrayList pool=new ArrayList();
// private List pool = Collections.synchronizedList(new ArrayList());// 线程连接存放容器
private int minThread = 20;// 最小的线程数
private int delayTime = 611;// 刷新线程容器的时间间隔
private int requestThread = 0;// 当前的线程数
private int incrementThreads = 2;// 每次添加的线程
public int getDelayTime() {
return delayTime;
}
public void setDelayTime(int delayTime) {
this.delayTime = delayTime;
}
public int getMinThread() {
return minThread;
}
public void setMinThread(int minThread) {
this.minThread = minThread;
}
public List getPool() {
return pool;
}
public void setPool(ArrayList pool) {
this.pool = pool;
}
public int getIncrementThreads() {
return incrementThreads;
}
public void setIncrementThreads(int incrementThreads) {
this.incrementThreads = incrementThreads;
}
public int getRequestThread() {
return requestThread;
}
public void setRequestThread(int requestThread) {
this.requestThread = requestThread;
}
public SaveReviewThreadPool() {
}
@SuppressWarnings("unchecked")
public void config() {
if (requestThread <= minThread) {
requestThread = minThread;
}
pool = new ArrayList();
for (int i = 0; i < requestThread; i++) {// 初始化线程连接池
SaveReviewThread thread = new SaveReviewThread();
pool.add(thread);
}
}
public SaveReviewThread getThread() {
if (pool == null) { // 如果线程容器为空则新建一个
pool = new ArrayList();
this.addThread();
}
SaveReviewThread thread = findThread();// 在此等候知道能够获得一个有用的线程
while (thread == null) {
wait(delayTime);
System.out.println("正在等候有用的文件保存线程");
thread = findThread();
}
System.out.println("成功获得一个文件保存线程");
return thread;
}
public SaveReviewThread findThread() {
SaveReviewThread thread = null;
synchronized (pool) {
Iterator iter = pool.iterator();// 对线程容器进行跌代,找出一个存在的线程
while (iter.hasNext()) {
thread = (SaveReviewThread) iter.next();
if (thread != null) {
break;
}
}
pool.remove(thread);
}
return thread;
}
@SuppressWarnings("unchecked")
public void addThread() {// 每次运行完一个进程,需要向进程池里添加的进程数量
synchronized (pool) {
if (pool.size() < requestThread) {
for (int i = 0; i < incrementThreads; i++) {
SaveReviewThread newThread = new SaveReviewThread();
pool.add(newThread);
if (pool.size() >= requestThread) {
return;
}
}
}
}
}
private void wait(int mSeconds) {
try {
Thread.sleep(mSeconds);
} catch (InterruptedException e) {
}
}
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class SaveReviewThreadPool {
private ArrayList pool=new ArrayList();
// private List pool = Collections.synchronizedList(new ArrayList());// 线程连接存放容器
private int minThread = 20;// 最小的线程数
private int delayTime = 611;// 刷新线程容器的时间间隔
private int requestThread = 0;// 当前的线程数
private int incrementThreads = 2;// 每次添加的线程
public int getDelayTime() {
return delayTime;
}
public void setDelayTime(int delayTime) {
this.delayTime = delayTime;
}
public int getMinThread() {
return minThread;
}
public void setMinThread(int minThread) {
this.minThread = minThread;
}
public List getPool() {
return pool;
}
public void setPool(ArrayList pool) {
this.pool = pool;
}
public int getIncrementThreads() {
return incrementThreads;
}
public void setIncrementThreads(int incrementThreads) {
this.incrementThreads = incrementThreads;
}
public int getRequestThread() {
return requestThread;
}
public void setRequestThread(int requestThread) {
this.requestThread = requestThread;
}
public SaveReviewThreadPool() {
}
@SuppressWarnings("unchecked")
public void config() {
if (requestThread <= minThread) {
requestThread = minThread;
}
pool = new ArrayList();
for (int i = 0; i < requestThread; i++) {// 初始化线程连接池
SaveReviewThread thread = new SaveReviewThread();
pool.add(thread);
}
}
public SaveReviewThread getThread() {
if (pool == null) { // 如果线程容器为空则新建一个
pool = new ArrayList();
this.addThread();
}
SaveReviewThread thread = findThread();// 在此等候知道能够获得一个有用的线程
while (thread == null) {
wait(delayTime);
System.out.println("正在等候有用的文件保存线程");
thread = findThread();
}
System.out.println("成功获得一个文件保存线程");
return thread;
}
public SaveReviewThread findThread() {
SaveReviewThread thread = null;
synchronized (pool) {
Iterator iter = pool.iterator();// 对线程容器进行跌代,找出一个存在的线程
while (iter.hasNext()) {
thread = (SaveReviewThread) iter.next();
if (thread != null) {
break;
}
}
pool.remove(thread);
}
return thread;
}
@SuppressWarnings("unchecked")
public void addThread() {// 每次运行完一个进程,需要向进程池里添加的进程数量
synchronized (pool) {
if (pool.size() < requestThread) {
for (int i = 0; i < incrementThreads; i++) {
SaveReviewThread newThread = new SaveReviewThread();
pool.add(newThread);
if (pool.size() >= requestThread) {
return;
}
}
}
}
}
private void wait(int mSeconds) {
try {
Thread.sleep(mSeconds);
} catch (InterruptedException e) {
}
}
}