一种线程交互模型的实现
2010-08-28 17:55
316 查看
本文介绍一种采用线程交互模型,即主线程执行队列的Task,其他线程投递Task进入主线程的任务队列,投递方式类似于Win32 SDK的PostMessage和SendMessage方法,提供异步投递和同步投递。
首先我们需要一个BlockAndAwaitableQueue类,该类的功能是提供除执行Task线程外的其他线程的任务投递功能,该类包含一个任务列表,即存放待执行的Task。同时要考虑到多线程的同步问题,需要采用同步锁进行同步。同时要保证BlockAndAwaitableQueue的主执行线程在无Task可执行时不占用过多的系统资源需要实现一种monitor模型。即在没有Task可执行时,需要使该线程进入等待队列中,而在有Task投入进来时,需要唤醒Task列表中的等待队列中的当前执行线程。该模型有个限制,即所提交的Task不应占用过多的执行时间。
Java代码
public class BlockAndAwaitableQueue{
private ArrayList taskList = new ArrayList();
private volatile boolean bStop = false;
private volatile boolean bRunning = false;
private Thread runningThread;
Runnable finalTask = new Runnable()
{
public void run()
{
System.out.println("runningThread will exit.");
synchronized (this) {
bRunning = false;
System.out.println("notify ...");
synchronized (runningThread) {
runningThread.notifyAll();
}
}
System.out.println("runningThread exit.");
}
};
public BlockAndAwaitableQueue()
{
}
/*
* This can be called by runningThread and other threads.
* @param task The task wants to be executed asychronized.
*/
public void addTask(Runnable task)
{
synchronized(taskList)
{
taskList.add(task);
// notify
taskList.notifyAll();
}
}
/*
* This can not be called by the runningThread
*/
public void addTaskAndAwait(final Runnable task)
{
if(runningThread == Thread.currentThread())
{
System.out.println("This can not be called by the runningThread");
return;
}
final Object latchObject = new Object();
synchronized(latchObject)
{
addTask(new Runnable(){
public void run()
{
task.run();
synchronized(latchObject)
{
latchObject.notify();
}
}
});
try {
latchObject.wait();
} catch (InterruptedException e) {
}
}
}
public void stopInvocation()
{
bStop = true;
addTask(finalTask);
}
public void stopInvocationAndAwait()
{
bStop = true;
addTaskAndAwait(finalTask);
}
private void doInvocations()
{
synchronized(taskList)
{
while(taskList.size() == 0)
{
try {
taskList.wait();
} catch (InterruptedException e) {
}
}
for(int i=0; i < taskList.size(); i++)
{
Runnable task = (Runnable)taskList.get(i);
task.run();
task = null;
}
// clear the task list
taskList.clear();
}
}
public void run()
{
synchronized (this) {
while(bRunning)
{
try {
System.out.println("await ...");
synchronized (runningThread) {
runningThread.wait();
}
System.out.println("await over");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
bRunning = true;
}
runningThread = Thread.currentThread();
bStop = false;
while(!bStop)
{
doInvocations();
}
}
public static void main(String[] args)
{
final BlockAndAwaitableQueue taskQueue = new BlockAndAwaitableQueue();
new Thread(new Runnable(){
public void run()
{
taskQueue.run();
}
}).start();
new Thread(new Runnable(){
public void run()
{
taskQueue.run();
}
}).start();
new Thread(new Runnable(){
public void run()
{
taskQueue.addTaskAndAwait(new Runnable(){
public void run()
{
System.out.println("Task ID#1 has been executed.");
}
});
System.out.println("After Task ID#1 has been executed.");
}
}).start();
new Thread(new Runnable(){
public void run()
{
taskQueue.addTask(new Runnable(){
public void run()
{
System.out.println("Task ID#2 has been executed.");
//stop the queue
}
});
}
}).start();
new Thread(new Runnable(){
public void run()
{
taskQueue.stopInvocationAndAwait();
taskQueue.stopInvocationAndAwait();
}
}).start();
}
}
相关文章推荐
- 一种模仿线程的Javascript异步模型设计&实现
- 一种利用线程池线程执行任务, 并能够结束超时任务的方法 (.NET实现)
- Exchanger 实现两个伙伴线程之间的数据交互
- 11.python并发入门(part8 基于线程队列实现生产者消费者模型)
- JAVA RMI线程模型及内部实现机制
- 多线程学习之AsyncOperation实现线程间交互
- Leader Follower线程模型简单实现
- 聊聊线程技术与线程实现模型
- Linux历史上线程的3种实现模型
- SpringMVC实现页面和java模型的数据交互以及文件上传下载和数据校验
- 基于线程实现的生产者消费者模型(Object.wait(),Object.notify()方法)
- Python3 多线程下实现C/S交互模型
- 多线程编程(二)——面试题,每个线程只打印一种字符,多个线程协同顺序打印n次字符串(求大神的其他实现方案)
- Linux的三种线程实现模型漫谈
- QT 线程暂停,继续执行的一种实现(有些道理,而且封装了)
- Java中的线程模型及实现方式
- activitygroup的子activity中嵌套webview,利用webview与js交互,在实现js点击的内部类代码中实现activity跳转出现的线程问题
- android线程 Handler Message Queue AsyncTask线程模型 线程交互 + 修改Button样式 示例 最终easy整合版
- 基于线程实现的生产者消费者模型(Object.wait(),Object.notify()方法)
- 多线程题 3个线程各打印一种字母,按顺序打印若干次ABC 的2种实现