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模式的核心则是它 ...
随机推荐
- vue之组件通信
vue组件通信一般分为以下几种情况: 1.父子组件通信: 2.兄弟组件通信: 3.跨多层级组件通信: 一.父子通信 父组件通过props传递数据给子组件,子组件通过emit发送事件传递数 ...
- solidworks 学习 (三)
汽车轮毂三维建模
- BZOJ 4477: [Jsoi2015]字符串树 可持久化trie树
这个是真——可持久化字典树..... code: #include <bits/stdc++.h> #define N 100006 #define setIO(s) freopen(s& ...
- AC自动机入门经典题目(两种表达方式)
Keywords Search 指针方式: /* Keywords Search */ # include <iostream> # include <stdio.h> # i ...
- @Aspect注解并不属于@Component的一种
也就是一个类单纯如果只添加了@Aspect注解,那么它并不能被context:component-scan标签扫描到. 想要被扫描到的话,需要追加一个@Component注解
- 微信小程序之页面导航栏
效果图: 页面有点丑,作为初次学习,页面可以要求不那么美观,先学会再说.毕竟后面可以优化的很漂亮. 代码实例如下: <view class="section btn-area" ...
- ldap和phpldapadmin的安装部署
LDAP 安装 一.安装LDAP 1. 安装包 yum install openssl-devel gcc libtool-ltdl-devel -y yum install openldap-ser ...
- 3ds Max学习日记(十一)——如何给模型上贴图
参考链接:https://jingyan.baidu.com/article/e4511cf38a810b2b845eaf1f.html 之前一直都不知道怎么在3dsMax里给模型上材质和贴图,被 ...
- B2C电子商务系统研发——商品SKU分析和设计(一)
一.SKU及相关概念定义 在设计商品SKU之前,首先让我们熟悉一下SKU和相关的一些概念. # 什么是SKU: SKU=Stock Keeping Unit(库存量单位) 同一型号的商品,或者说是同一 ...
- curl保留cookie
curl -c cookie.txt http://localhost:9001/login.json?c=65CE13E16CF394D curl -b @cookie.txt http://loc ...