任务池
package com.task;
/**
* 任务类
* @author jiangl
*
*/
public interface ITask{
public void execute();
}
package com.task;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import java.util.concurrent.ConcurrentLinkedQueue;
/**
* 执行器队列
* @author jiangl
*
*/
public class ExecuterQueue {
private int size = -1;
private String name = null;
private boolean isdaemon;
/**
*
* @param size 执行器线程池大小
* @param isdaemon 执行器线程池中的线程是否为守护线程
* @param name 执行器线程池名字
*/
public ExecuterQueue(int size, boolean isdaemon, String name){
this.size = size;
this.name = name;
this.isdaemon = isdaemon;
init();
}
private void init(){
for(int i=0; i<size; i++){
Executer exe = new Executer(this);
queue.add(exe);
exe.setDaemon(isdaemon);
exe.start();
}
}
public String getExecuterQueueName(){
return this.name;
}
private Vector<Executer> queue = new Vector<Executer>();
private ConcurrentLinkedQueue<ITask> list = new ConcurrentLinkedQueue<ITask>();
public synchronized ConcurrentLinkedQueue<ITask> getLastTask(){
return list;
}
// 添加一项任务
public synchronized void process(ITask task) throws InterruptedException {
if (task != null) {
if(queue.size() > 0){
Executer exe = queue.remove(0);
exe.setIsRunning(true);
exe.process(task);
}else{
list.add(task);
}
}
}
// 完成任务后将它从任务队列中删除
public synchronized void finishTask(Executer exe) {
if (exe != null) {
exe.setIsRunning(false);
queue.add(exe);
}
}
}
package com.task;
import java.lang.Runnable;
/**
*
* @author jiangl
*
*/
public class Executer extends Thread {
private ITask task;
private ExecuterQueue target;
private boolean running = false;
public Executer(ExecuterQueue target) {
this.target = target;
}
public void process(ITask task) {
this.task = task;
synchronized (this) {
this.notify(); // 恢复线程
}
}
public boolean getIsRunning() {
return this.running;
}
public void setIsRunning(boolean boo) {
this.running = boo;
}
@Override
public void run() {
try {
while (true) {
if (task == null || !this.getIsRunning()) {
synchronized (this) {
try {
// System.out.println("Executer.class wait()");
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
task.execute(); //执行
if ((task = target.getLastTask().poll()) != null) {
// System.out.println("get old task");
continue;
}
target.finishTask(this);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.task;
import com.util.StringUtil;
/**
* 执行器线程池管理器
* @author jiangl
*
*/
public class ExecuterManager {
private static ExecuterQueue queue = null;
public ExecuterManager(){}
/**
*
* @param isdaemon 执行器内的执行器(线程)是否为守护线程
*/
public static void init(boolean isdaemon){
queue = new ExecuterQueue(5, isdaemon, StringUtil.SYSTEM);
}
public static void process(ITask task){
try {
queue.process(task);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com;
import com.task.ITask;
public class DemoTask implements ITask {
private String msg;
public DemoTask(String msg){
this.msg = msg;
}
@Override
public void execute() {
System.out.println(">>>("+msg+")>>>Thread:>>>" + Thread.currentThread().getName());
}
}
package com;
import com.task.ExecuterManager;
/**
*
* @author jiangl
*
*/
public class TestTask {
/**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
ExecuterManager.init(false);
DemoTask task = null;
for (int i = 0; i < 3000000; i++) {
task = new DemoTask("test" + i);
ExecuterManager.process(task);
}
}
}
下载地址点击打开链接