前言:最近做的一个项目正好要用到多线程,而且要实现队列式处理,即外界可向线程添加任务,同时一旦队列中有任务时,线程自动启动对之进行处理。要求任务的添加与执行要同步处理。最终结果如下:
[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();
}
以上代码都已经过测试,呵呵,一个简单的线程雏形,希望对初学者有所帮助,算是抛砖引玉吧。