您的位置:首页 > 编程语言 > Java开发

Java线程池

2016-05-16 20:55 441 查看

简单实现线程池

Java线程池的分析与使用

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;

/**
* Created by wenqing on 2016/5/16.
*/
public class ThreadPool {
//阻塞队列
private BlockingQueue<Runnable> taskQueue;
private List<PoolThread> threads;
private boolean isStopped = false;

public ThreadPool(int threadSize,int runnableSize) {
threads = new ArrayList<PoolThread>(threadSize);
taskQueue = new LinkedBlockingDeque<Runnable>(runnableSize) ;
for(int i = 0;i < threadSize; i++) {
PoolThread thread = new PoolThread(taskQueue);
threads.add(thread);
}
for (PoolThread thread : threads) {
thread.start();
}
}

public void execute(Runnable runnable) {
if(isStopped)
throw new IllegalStateException("ThreadPool is stopped");
taskQueue.add(runnable);
}

public void stop() {
if(!isStopped) {
isStopped = true;
for (PoolThread thread : threads) {
thread.toStop();
}
}
}

}

class PoolThread extends Thread {
private BlockingQueue<Runnable> taskQueue;
private boolean isStoped = false;

public PoolThread(BlockingQueue<Runnable> taskQueue) {
this.taskQueue = taskQueue;
}

@Override
public void run() {
while (!isStoped) {
try {
Runnable runnable = taskQueue.take();
runnable.run();
} catch (InterruptedException e) {
// 写日志或者报告异常,
// 但保持线程池运行.
}
}
}

public void toStop() {
isStoped = true;
this.interrupt();
}

public boolean isStoped() {
return isStoped;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: