[原创]多线程之队列式执行任务

      前言:最近做的一个项目正好要用到多线程,而且要实现队列式处理,即外界可向线程添加任务,同时一旦队列中有任务时,线程自动启动对之进行处理。要求任务的添加与执行要同步处理。最终结果如下:

[TaskThread.java]

import java.util.HashMap;
import java.util.Vector;

/**
* Task Processing Thread.It is enable to add Task and perform task synchronously.
* All tasks is restored in the task queue.
* If there is task(s) in the task queue,it(them) will be performed by the thread automaticlly.
* Or the thread will be turned into waiting for waking.
* <br>
* Mode:Multiton Mode
* <br>
* 任务处理线程,实现同步添加及执行任务,任务保存在任务队列之中.
* 当任务队列中存在任务时,线程将自动对其进行处理;否则线程进入等待状态.
* <br>
* 模式:多例模式
* @author Micheal Hong
* @email babala_234@163.com
* @see Task
*/
public class TaskThread extends Thread{

private static HashMap<String,TaskThread> instanceMap=new HashMap<String,TaskThread>();

private Vector<Task> taskQueue=new Vector<Task>();
private boolean runnable=true;

/**
* Return a task thread instance corresponding to threadId,
* if it don't exist,create one and return it.
* Note that each <code>threadId</code> is unique to the others.
* <br>
* 返回一个与threadId对应的任务处理线程实例,如果不存在这样的实例,
* 则进行创建并返回一个新实例,新实例与threadId相对应
* @param threadId
* @return
*/
public static TaskThread getInstance(String threadId)
{
   if(instanceMap.containsKey(threadId)==false)
   {
    instanceMap.put(threadId,new TaskThread());
   }
   return instanceMap.get(threadId);
}

private TaskThread()
{}

@Override
public void run() {
   Task task;
   while(runnable)
   {
    synchronized (taskQueue) {   //apply for the task queue's obj lock
     while(taskQueue.isEmpty())
     {
      taskQueue.notify();   //release the task queue's obj lock
      try {
       taskQueue.wait(); //turn into waiting for being waken
      } catch (InterruptedException e) {
       e.printStackTrace();
      }
     }
     /*when the task queue is not empty*/
     task=taskQueue.remove(0); //fetch the head task
     taskQueue.notify();    //release the task queue's obj lock
    }
    task.perform();      //perform the task
   }
}

/**
* Add task into task queue synchronously and then wake the task thread.
* <br>
* 向任务队列添加任务并唤醒线程对队列进行处理
* @param task
*/
public void addTask(Task task)
{
   synchronized (taskQueue) {   //apply for the task queue's obj lock
    taskQueue.add(task);
    taskQueue.notify();    //release the task queue's obj lock
   }
}

}

 

[Task.java]

/**
* Task Entry
* 任务实体
* Mode:Template Mode
* 模式:模板模式
* @author Micheal Hong
*
*/
public abstract class Task {
protected String projectId;

public Task(String projectId)
{
   this.projectId=projectId;
}

public String getProjectId() {
   return projectId;
}

/**
* task performance.It is an interface supplied for implemented by caller.
* 任务的执行。由调用者自行实现。
* @see TaskThread
*/
public abstract void perform();

}

 

以上代码都已经过测试,呵呵,一个简单的线程雏形,希望对初学者有所帮助,算是抛砖引玉吧。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值