java 任务池_简单 任务调度 任务池 | 学步园

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

Executer exe = new Executer(this);

queue.add(exe);

exe.setDaemon(isdaemon);

exe.start();

}

}

public String getExecuterQueueName(){

return this.name;

}

private Vector queue = new Vector();

private ConcurrentLinkedQueue list = new ConcurrentLinkedQueue();

public synchronized ConcurrentLinkedQueue 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);

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值