线程池的实现主要参考了孙卫琴的Java网络编程精解一书 import java.util.HashMap; import java.util.LinkedList; import org.apache.mina.core.buffer.IoBuffer; import cn.edu.zju.cst.mina.im.server.entity.Group; import cn.edu.zju.cst.mina.im.server.entity.Piece; import cn.edu.zju.cst.mina.im.server.util.Configuration; public class GroupManage extends ThreadGroup { private static boolean isClosed = false; private HashMap<Long, Group> groupList; private LinkedList<Long> workQueue; private int threadID; private static int threadPoolID; private int maxPoolSize; public synchronized void execute(long uniqueID, Piece newPiece) { if (isClosed) { System.out.println("线程池已经关闭,添加新任务失败!"); return; } System.out.println("添加新任务:"+uniqueID); if (newPiece != null) { if (!groupList.containsKey(uniqueID)) { System.out.println("该包的第一个分片到达:"+uniqueID); Group newGroup = new Group(); byte[] desData = new byte[newPiece.getTotalLenth() - newPiece.getTotal()* Configuration.getConfInfo("groupHeadLenth")]; byte[] srcData = newPiece.getData(); int desPoistion = newPiece.getIndex()* Configuration.getConfInfo("messageLenth"); System.arraycopy(srcData, 0, desData, desPoistion,srcData.length); newGroup.setData(desData); newGroup.setNextFilter(newPiece.getNextFilter()); newGroup.getRecivedPiceIndexList().add(newPiece.getIndex()); newGroup.setSession(newPiece.getSession()); newGroup.setTotalLenth(newPiece.getTotalLenth()); newGroup.setTotle(newPiece.getTotal()); newGroup.setUniqueID(newPiece.getUniqueID()); groupList.put(uniqueID, newGroup); workQueue.add(uniqueID); } else { Group desGroup = groupList.get(uniqueID); byte[] desData = desGroup.getData(); byte[] srcData = newPiece.getData(); int desPoistion = newPiece.getIndex()* Configuration.getConfInfo("messageLenth"); System.arraycopy(srcData, 0, desData, desPoistion,srcData.length); } notifyAll(); } else { } } public synchronized Group getTask() throws InterruptedException { while (workQueue.isEmpty()) { if (isClosed) { System.out.println("当前任务序列为空!"); return null; } wait(); } return groupList.get(workQueue.removeFirst()); } /** * 构造函数1 */ public GroupManage() { this(-1, -1); // this()必须发在第一行 } /** * 构造函数2 * * @param maxPoolSize * 线程池的最大线程数 */ public GroupManage(int maxPoolSize) { this(-1, maxPoolSize); } /** * 构造函数3 * * @param minPoolSize * 线程池的初始线程数 * @param maxPoolSize * 线程池的最大线程数 */ public GroupManage(int minPoolSize, int maxPoolSize) { super("ThreadPool" + (threadPoolID++)); setDaemon(true); groupList = new HashMap<Long, Group>(); workQueue = new LinkedList<Long>(); if (minPoolSize < 0) { minPoolSize = Configuration.getConfInfo("minPoolSize"); } else if (maxPoolSize < 0) { maxPoolSize = Configuration.getConfInfo("maxPoolSize"); this.maxPoolSize = maxPoolSize; } for (int i = 0; i < minPoolSize; i++) { new WorkThread(20000).start(); } } public synchronized boolean isFirst(long uniqueID) { return groupList.containsKey(uniqueID); } /** * 内部类,工作线程 * */ private class WorkThread extends Thread { private int timeout; private Group taskGroup; private int threadId; public WorkThread(int timeout ) { super(GroupManage.this, "WorkThrea" + (threadID++)); threadId = threadID - 1; System.out.println("工作线程"+threadId+"创建,其超时间隔为:"+timeout); this.timeout = timeout; } public void run() { System.out.println("开始RUN"+threadId); while (!isInterrupted()) { //取一个任务 try { taskGroup = getTask(); } catch (Exception e) { System.out.println("取任务时遇到问题"); } if (taskGroup == null) { System.out.println("取到的任务为空"); return; } System.out.println("**************任务状态正常,开始工作*******************"); //得到任务后开始工作 try { long startTime = System.currentTimeMillis(); long id = taskGroup.getUniqueID(); while (!taskGroup.isFull()) { long endTime = System.currentTimeMillis(); if((endTime - startTime) > timeout){ System.out.println("***************分组"+id+":超时结束!***************"); groupList.remove(id); continue; } } IoBuffer buffer = IoBuffer.wrap(taskGroup.getData()); System.out.println("***************分组"+id+":组包完成,交给下一层处理,同时清空缓存!***************"); taskGroup.getNextFilter().messageReceived(taskGroup.getSession(),buffer); groupList.remove(id); } catch (Throwable e) { e.printStackTrace(); } } } } }