Active Objects模式
实现的思路是,通过代理将方法的调用转变为向阻塞队列中添加一个请求,由一个线程取出请求后执行实际的方法,然后将结果设置到Future中

这里用到了代理模式,Future模式
/**************************************
* 接受异步消息的主动对象
*
**/
public interface ActiveObject { Result makeString(int count,char fillChar); void displayString(String text);
}
/*
* 主动对象的实现类
*
*/
class Servant implements ActiveObject { @Override
public Result makeString(int count, char fillChar) {
char[] buf = new char[count];
for (int i = ; i < count; i++) {
buf[i] = fillChar;
}
try {
Thread.sleep();
} catch (Exception e) { }
return new RealResult(new String(buf));
} @Override
public void displayString(String text) {
try {
Thread.sleep();
System.out.println("Display:" + text);
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 主动对象的代理类
*
*/
class ActiveObjectProxy implements ActiveObject { private final SchedulerThread schedulerThread; private final Servant servant; public ActiveObjectProxy(SchedulerThread schedulerThread, Servant servant) {
this.schedulerThread = schedulerThread;
this.servant = servant;
} @Override
public Result makeString(int count, char fillChar) {
FutureResult future = new FutureResult();
//往队列里添加调用请求MethodRequest
schedulerThread.invoke(new MakeStringRequest(servant, future, count, fillChar));
return future;
} @Override
public void displayString(String text) {
schedulerThread.invoke(new DisplayStringRequest(servant, text));
}
}
/**
* 工具类
*/
public final class ActiveObjectFactory { private ActiveObjectFactory() {
} public static ActiveObject createActiveObject() {
Servant servant = new Servant();
ActivationQueue queue = new ActivationQueue();
SchedulerThread schedulerThread = new SchedulerThread(queue);
ActiveObjectProxy proxy = new ActiveObjectProxy(schedulerThread,servant);
schedulerThread.start();
return proxy;
}
}
/**
*
* 调度者,从队列里取出任务并执行
*
*/
public class SchedulerThread extends Thread { private final ActivationQueue activationQueue; public SchedulerThread(ActivationQueue activationQueue) {
this.activationQueue = activationQueue;
} public void invoke(MethodRequest request) {
this.activationQueue.put(request);
} @Override
public void run() {
while (true) {
activationQueue.take().execute();
}
}
}
/***
* 阻塞队列
*/
public class ActivationQueue { private final static int DEFAULT_SIZE = ; private final LinkedList<MethodRequest> methodQueue; public ActivationQueue() {
methodQueue = new LinkedList<>();
} public synchronized void put(MethodRequest request) {
while (methodQueue.size() >= DEFAULT_SIZE) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.methodQueue.addLast(request);
this.notifyAll();
} public synchronized MethodRequest take() {
while (methodQueue.isEmpty()) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
MethodRequest methodRequest = methodQueue.removeFirst();
this.notifyAll();
return methodRequest;
}
}
/**
* 返回结果
*
*/
public interface Result { Object getResultValue(); }
/**
* 返回的对象
*/
public class RealResult implements Result { private final Object resultValue; public RealResult(Object resultValue) {
this.resultValue = resultValue;
} @Override
public Object getResultValue() {
return this.resultValue;
}
}
/**
*
* 实际返回给客户的result
*
*/
public class FutureResult implements Result { private Result result; private boolean ready = false; public synchronized void setResult(Result result) {
this.result = result;
this.ready = true;
this.notifyAll();
} @Override
public synchronized Object getResultValue() {
while (!ready) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return this.result.getResultValue();
}
}
/**
* 调用请求
*/
public abstract class MethodRequest { protected final Servant servant; protected final FutureResult futureResult; public MethodRequest(Servant servant, FutureResult futureResult) {
this.servant = servant;
this.futureResult = futureResult;
} public abstract void execute();
}
/**
*
* 拼接字符
*/
public class MakeStringRequest extends MethodRequest { private final int count;
private final char fillChar; public MakeStringRequest(Servant servant, FutureResult futureResult, int count, char fillChar) {
super(servant, futureResult);
this.fillChar = fillChar;
this.count = count;
} @Override
public void execute() {
Result result = servant.makeString(count, fillChar);
futureResult.setResult(result);
}
}
/**
* 打印字符串
*
*/
public class DisplayStringRequest extends MethodRequest { private final String text; public DisplayStringRequest(Servant servant, final String text) {
super(servant, null);
this.text = text;
} @Override
public void execute() {
this.servant.displayString(text);
}
}
public class Test {
public static void main(String[] args) {
ActiveObject activeObject = ActiveObjectFactory.createActiveObject();
activeObject.displayString("VVVVVVVVVV");
Result makeString = activeObject.makeString(, 'A');
System.out.println("#################");
System.out.println(makeString.getResultValue());
}
}
Active Objects模式的更多相关文章
- Java多线程编程模式实战指南:Active Object模式(下)
Active Object模式的评价与实现考量 Active Object模式通过将方法的调用与执行分离,实现了异步编程.有利于提高并发性,从而提高系统的吞吐率. Active Object模式还有个 ...
- java Active Object模式(下)
Active Object模式的评价与实现考量 Active Object模式通过将方法的调用与执行分离,实现了异步编程.有利于提高并发性,从而提高系统的吞吐率. Active Object模式还有个 ...
- Java多线程编程模式实战指南(一):Active Object模式--转载
本文由黄文海首次发布在infoq中文站上:http://www.infoq.com/cn/articles/Java-multithreaded-programming-mode-active-obj ...
- 敏捷软件开发(3)---COMMAND 模式 & Active Object 模式
COMMAND 模式 command模式非常简单,简单到你无法想象的地方. public interface Command { void execute(); } 这就是一个command模式的样子 ...
- Android开源库--ActiveAndroid(active record模式的ORM数据库框架)
Github地址:https://github.com/pardom/ActiveAndroid 前言 我一般在Android开发中,几乎用不到SQLlite,因为一些小数据就直接使用Preferen ...
- Java多线程编程模式实战指南:Active Object模式(上)
Active Object模式简介 Active Object模式是一种异步编程模式.它通过对方法的调用与方法的执行进行解耦来提高并发性.若以任务的概念来说,Active Object模式的核心则是它 ...
- java Active Object模式(上)
Active Object模式简介 Active Object模式是一种异步编程模式.它通过对方法的调用与方法的执行进行解耦来提高并发性.若以任务的概念来说,Active Object模式的核心则是它 ...
- Java多线程编程模式实战指南一:Active Object模式(下)
Active Object模式的评价与实现考量 Active Object模式通过将方法的调用与执行分离,实现了异步编程.有利于提高并发性,从而提高系统的吞吐率. Active Object模式还有个 ...
- Java多线程编程模式实战指南一:Active Object模式(上)
Active Object模式简介 Active Object模式是一种异步编程模式.它通过对方法的调用与方法的执行进行解耦来提高并发性.若以任务的概念来说,Active Object模式的核心则是它 ...
随机推荐
- window对象(全局对象)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- LeetCode 1026. Maximum Difference Between Node and Ancestor
原题链接在这里:https://leetcode.com/problems/maximum-difference-between-node-and-ancestor/ 题目: Given the ro ...
- LeetCode 1008. Construct Binary Search Tree from Preorder Traversal
原题链接在这里:https://leetcode.com/problems/construct-binary-search-tree-from-preorder-traversal/ 题目: Retu ...
- solidworks 学习 (三)
汽车轮毂三维建模
- UFUN函数 UF_ATTR函数(UF_ATTR_cycle )
UF_initialize(); tag_t ; ; int type=UF_ATTR_any ; ]=""; UF_ATTR_value_t value; //循环读取程序的属性 ...
- zeptojs库
一.简介 ①Zepto是一个轻量级的针对现代高级浏览器的JavaScript库, 它与jquery有着类似的api. ②Zepto的设计目的是提供 jQuery 的类似的API,但并不是100%覆盖 ...
- 【dp】P1064 金明的预算方案
题目描述 金明今天很开心,家里购置的新房就要领钥匙了,新房里有一间金明自己专用的很宽敞的房间.更让他高兴的是,妈妈昨天对他说:“你的房间需要购买哪些物品,怎么布置,你说了算,只要不超过NN元钱就行”. ...
- 微信小程序搜索框代码组件
search.wxml <view class="header"> <view class="search"> <icon typ ...
- es6学习4:async和await
async async函数返回一个 Promise 对象,可以使用then方法添加回调函数.当函数执行的时候,一旦遇到await就会先返回,等到异步操作完成,再接着执行函数体内后面的语句. funct ...
- 用户账户——《Python编程从入门到实践》
Web应用程序的核心是让任何用户都能够注册账户并能够使用它,不管用户身处何方 1.让用户能够输入数据 建立用于创建用户的身份验证系统之前,我们先来添加几个页面,让用户能够输入数据.当前,只有超级用户能 ...