简单 任务调度 任务池

任务池
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);
		}
		
	}
	

}


下载地址点击打开链接





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

乌拉乌拉liang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值