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)); } } }