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模式的核心则是它 ...
随机推荐
- Making Huge Palindromes LightOJ - 1258
题目链接:LightOJ - 1258 1258 - Making Huge Palindromes PDF (English) Statistics Forum Time Limit: 1 se ...
- 数据库服务器和web服务器磁盘占用查询
对于Oracle数据库而言磁盘空间主要体现在表空间上,可使用sql语句进行查看Oracle 表空间的大小及使用情况: select sum(bytes)/1024/1024/1024 "Gb ...
- Dump文件定制工具---MiniDump Wizard
MiniDump向导应用程序允许在不编写代码的情况下尝试MiniDumpWriteDump和MiniDumpCallback函数.可以指定将传递给MiniDumpWriteDump函数的MINIDUM ...
- vue-cli之路由独立成JS文件之后,如何在路由中获取vuex属性或者设置国际化i18n的当前使用语言
国际化vue-i18n的使用: import Vue from 'vue'; import VueI18n from 'vue-i18n'; // 引入语言包 import zh from '@/co ...
- (7)Go切片
切片 切片(Slice)是一个拥有相同类型元素的可变长度的序列.它是基于数组类型做的一层封装.它非常灵活,支持自动扩容. 切片是一个引用类型,它的内部结构包含地址.长度和容量.切片一般用于快速地操作一 ...
- C++通过迭代修改字符串本身(auto类型说明符)
以字符串这种支持 for (declaration : expression) statement 这样for语句迭代的数据结构为例,我们看看auto关键字在类型推断中的作用. string s = ...
- [HAOI2018]染色(NTT)
前置芝士 可重集排列 NTT 前置定义 \[\begin{aligned}\\ f_i=C_m^i\cdot \frac{n!}{(S!)^i(n-iS)!}\cdot (m-i)^{n-iS}\\ ...
- bluestart
# Add nano as default editorexport EDITOR=nanoexport PULSE_LATENCY_MSEC=60 alias ls='ls --color=auto ...
- 2019暑假Java学习笔记(一)
目录 基础语法(上) HelloWorld 变量 常量 数据类型 整数 浮点数 char类型 boolean类型 String 计算字符串长度 字符串比较 字符串连接 charAt()方法 字符串常用 ...
- #C++初学记录(ACM8-6-cf-f题)
F. Vanya and Label While walking down the street Vanya saw a label "Hide&Seek". Becaus ...