标签:线程池
package com.mzsx.concurrent.threadpool;
import java.util.List;
import java.util.Vector;
public class ThreadPool {
private static ThreadPool instance=null;
//空闲的线程队列
private List<PThread> idleThreads;
//已有的线程总数
private int threadCounter;
private boolean isShutdown;
private ThreadPool(){
this.idleThreads=new Vector<PThread>(5);
threadCounter=0;
}
public int getCreatedThreadsCount(){
return threadCounter;
}
//取得线程池的实例
public synchronized static ThreadPool getInstance(){
if (instance==null) {
instance=new ThreadPool();
}
return instance;
}
//放入线程池
protected synchronized void repool(PThread repoolingThread){
if (!isShutdown) {
idleThreads.add(repoolingThread);
}else{
repoolingThread.shutdown();
}
}
//停止线程池中所有的线程
public synchronized void shutDown(){
isShutdown=true;
for (int i = 0; i < idleThreads.size(); i++) {
PThread idleThread=(PThread)idleThreads.get(i);
idleThread.shutdown();
}
}
//执行任务
public synchronized void start(Runnable target){
PThread thread=null;
//如果有空闲的线程,则直接使用
if (idleThreads.size()>0) {
int lastIndex=idleThreads.size()-1;
thread=idleThreads.get(lastIndex);
//立即执行这个任务
thread.setTarget(target);
}else {
threadCounter++;
//创建新线程
thread =new PThread(target, "PThread#"+threadCounter, this);
thread.start();
}
}
}package com.mzsx.concurrent.threadpool;
public class PThread extends Thread {
//线程池
private ThreadPool pool;
//任务
private Runnable target;
private boolean isSHutdown=false;
private boolean isIdle=false;
public PThread(Runnable target,String name,ThreadPool pool){
super(name);
this.pool=pool;
this.target=target;
}
public ThreadPool getPool() {
return pool;
}
public void setPool(ThreadPool pool) {
this.pool = pool;
}
public Runnable getTarget() {
return target;
}
public synchronized void setTarget(Runnable newTarget) {
target = newTarget;
notifyAll();
}
public boolean isSHutdown() {
return isSHutdown;
}
public void setSHutdown(boolean isSHutdown) {
this.isSHutdown = isSHutdown;
}
public boolean isIdle() {
return isIdle;
}
public void setIdle(boolean isIdle) {
this.isIdle = isIdle;
}
@Override
public void run() {
//只要没有关闭,则一直不结束该线程
while(!isSHutdown){
isIdle=false;
if (target!=null) {
//运行任务
target.run();
}
//任务结束,到闲置状态
isIdle=true;
try {
//该线程任务结束后,不关闭线程,而是放入线程池空闲对了
pool.repool(this);
synchronized (this) {
wait();
}
} catch (Exception e) {
// TODO: handle exception
}
isIdle=false;
}
}
//关闭线程
public synchronized void shutdown(){
isSHutdown=true;
notifyAll();
}
}
package com.mzsx.concurrent.threadpool;
public class MyThread implements Runnable {
protected String name;
public MyThread() {
}
public MyThread(String name) {
super();
this.name = name;
}
@Override
public void run() {
try {
System.out.println(this.name);
Thread.sleep(1);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}package com.mzsx.concurrent.threadpool;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//不使用线程池
/*for (int i = 0; i < 1000; i++) {
new Thread(new MyThread("MyThread-"+i)).start();
}*/
//使用线程池
/*for (int i = 0; i < 1000; i++) {
ThreadPool.getInstance().start(new MyThread("MyThread-"+i));
}*/
//JDK自带的线程池
ExecutorService executorService=Executors.newCachedThreadPool();
for (int i = 0; i < 1000; i++) {
executorService.execute(new MyThread("MyThread-"+i));
}
}
}
本文出自 “梦朝思夕” 博客,请务必保留此出处http://qiangmzsx.blog.51cto.com/2052549/1420962
标签:线程池
原文地址:http://qiangmzsx.blog.51cto.com/2052549/1420962