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

这里用到了代理模式,Future模式

  1. /**************************************
  2. * 接受异步消息的主动对象
  3. *
  4. **/
  5. public interface ActiveObject {
  6.  
  7. Result makeString(int count,char fillChar);
  8.  
  9. void displayString(String text);
  10. }
  1. /*
  2. * 主动对象的实现类
  3. *
  4. */
  5. class Servant implements ActiveObject {
  6.  
  7. @Override
  8. public Result makeString(int count, char fillChar) {
  9. char[] buf = new char[count];
  10. for (int i = ; i < count; i++) {
  11. buf[i] = fillChar;
  12. }
  13. try {
  14. Thread.sleep();
  15. } catch (Exception e) {
  16.  
  17. }
  18. return new RealResult(new String(buf));
  19. }
  20.  
  21. @Override
  22. public void displayString(String text) {
  23. try {
  24. Thread.sleep();
  25. System.out.println("Display:" + text);
  26. } catch (Exception e) {
  27. e.printStackTrace();
  28. }
  29. }
  30. }
  1. /**
  2. * 主动对象的代理类
  3. *
  4. */
  5. class ActiveObjectProxy implements ActiveObject {
  6.  
  7. private final SchedulerThread schedulerThread;
  8.  
  9. private final Servant servant;
  10.  
  11. public ActiveObjectProxy(SchedulerThread schedulerThread, Servant servant) {
  12. this.schedulerThread = schedulerThread;
  13. this.servant = servant;
  14. }
  15.  
  16. @Override
  17. public Result makeString(int count, char fillChar) {
  18. FutureResult future = new FutureResult();
  19. //往队列里添加调用请求MethodRequest
  20. schedulerThread.invoke(new MakeStringRequest(servant, future, count, fillChar));
  21. return future;
  22. }
  23.  
  24. @Override
  25. public void displayString(String text) {
  26. schedulerThread.invoke(new DisplayStringRequest(servant, text));
  27. }
  28. }
  1. /**
  2. * 工具类
  3. */
  4. public final class ActiveObjectFactory {
  5.  
  6. private ActiveObjectFactory() {
  7. }
  8.  
  9. public static ActiveObject createActiveObject() {
  10. Servant servant = new Servant();
  11. ActivationQueue queue = new ActivationQueue();
  12. SchedulerThread schedulerThread = new SchedulerThread(queue);
  13. ActiveObjectProxy proxy = new ActiveObjectProxy(schedulerThread,servant);
  14. schedulerThread.start();
  15. return proxy;
  16. }
  17. }
  1. /**
  2. *
  3. * 调度者,从队列里取出任务并执行
  4. *
  5. */
  6. public class SchedulerThread extends Thread {
  7.  
  8. private final ActivationQueue activationQueue;
  9.  
  10. public SchedulerThread(ActivationQueue activationQueue) {
  11. this.activationQueue = activationQueue;
  12. }
  13.  
  14. public void invoke(MethodRequest request) {
  15. this.activationQueue.put(request);
  16. }
  17.  
  18. @Override
  19. public void run() {
  20. while (true) {
  21. activationQueue.take().execute();
  22. }
  23. }
  24. }
  1. /***
  2. * 阻塞队列
  3. */
  4. public class ActivationQueue {
  5.  
  6. private final static int DEFAULT_SIZE = ;
  7.  
  8. private final LinkedList<MethodRequest> methodQueue;
  9.  
  10. public ActivationQueue() {
  11. methodQueue = new LinkedList<>();
  12. }
  13.  
  14. public synchronized void put(MethodRequest request) {
  15. while (methodQueue.size() >= DEFAULT_SIZE) {
  16. try {
  17. this.wait();
  18. } catch (InterruptedException e) {
  19. e.printStackTrace();
  20. }
  21. }
  22. this.methodQueue.addLast(request);
  23. this.notifyAll();
  24. }
  25.  
  26. public synchronized MethodRequest take() {
  27. while (methodQueue.isEmpty()) {
  28. try {
  29. this.wait();
  30. } catch (InterruptedException e) {
  31. e.printStackTrace();
  32. }
  33. }
  34. MethodRequest methodRequest = methodQueue.removeFirst();
  35. this.notifyAll();
  36. return methodRequest;
  37. }
  38. }
  1. /**
  2. * 返回结果
  3. *
  4. */
  5. public interface Result {
  6.  
  7. Object getResultValue();
  8.  
  9. }
  1. /**
  2. * 返回的对象
  3. */
  4. public class RealResult implements Result {
  5.  
  6. private final Object resultValue;
  7.  
  8. public RealResult(Object resultValue) {
  9. this.resultValue = resultValue;
  10. }
  11.  
  12. @Override
  13. public Object getResultValue() {
  14. return this.resultValue;
  15. }
  16. }
  1. /**
  2. *
  3. * 实际返回给客户的result
  4. *
  5. */
  6. public class FutureResult implements Result {
  7.  
  8. private Result result;
  9.  
  10. private boolean ready = false;
  11.  
  12. public synchronized void setResult(Result result) {
  13. this.result = result;
  14. this.ready = true;
  15. this.notifyAll();
  16. }
  17.  
  18. @Override
  19. public synchronized Object getResultValue() {
  20. while (!ready) {
  21. try {
  22. this.wait();
  23. } catch (InterruptedException e) {
  24. e.printStackTrace();
  25. }
  26. }
  27. return this.result.getResultValue();
  28. }
  29. }
  1. /**
  2. * 调用请求
  3. */
  4. public abstract class MethodRequest {
  5.  
  6. protected final Servant servant;
  7.  
  8. protected final FutureResult futureResult;
  9.  
  10. public MethodRequest(Servant servant, FutureResult futureResult) {
  11. this.servant = servant;
  12. this.futureResult = futureResult;
  13. }
  14.  
  15. public abstract void execute();
  16. }
  1. /**
  2. *
  3. * 拼接字符
  4. */
  5. public class MakeStringRequest extends MethodRequest {
  6.  
  7. private final int count;
  8. private final char fillChar;
  9.  
  10. public MakeStringRequest(Servant servant, FutureResult futureResult, int count, char fillChar) {
  11. super(servant, futureResult);
  12. this.fillChar = fillChar;
  13. this.count = count;
  14. }
  15.  
  16. @Override
  17. public void execute() {
  18. Result result = servant.makeString(count, fillChar);
  19. futureResult.setResult(result);
  20. }
  21. }
  1. /**
  2. * 打印字符串
  3. *
  4. */
  5. public class DisplayStringRequest extends MethodRequest {
  6.  
  7. private final String text;
  8.  
  9. public DisplayStringRequest(Servant servant, final String text) {
  10. super(servant, null);
  11. this.text = text;
  12. }
  13.  
  14. @Override
  15. public void execute() {
  16. this.servant.displayString(text);
  17. }
  18. }
  1. public class Test {
  2.  
  3. public static void main(String[] args) {
  4.  
  5. ActiveObject activeObject = ActiveObjectFactory.createActiveObject();
  6. activeObject.displayString("VVVVVVVVVV");
  7. Result makeString = activeObject.makeString(, 'A');
  8. System.out.println("#################");
  9. System.out.println(makeString.getResultValue());
  10.  
  11. }
  12. }

Active Objects模式的更多相关文章

  1. Java多线程编程模式实战指南:Active Object模式(下)

    Active Object模式的评价与实现考量 Active Object模式通过将方法的调用与执行分离,实现了异步编程.有利于提高并发性,从而提高系统的吞吐率. Active Object模式还有个 ...

  2. java Active Object模式(下)

    Active Object模式的评价与实现考量 Active Object模式通过将方法的调用与执行分离,实现了异步编程.有利于提高并发性,从而提高系统的吞吐率. Active Object模式还有个 ...

  3. Java多线程编程模式实战指南(一):Active Object模式--转载

    本文由黄文海首次发布在infoq中文站上:http://www.infoq.com/cn/articles/Java-multithreaded-programming-mode-active-obj ...

  4. 敏捷软件开发(3)---COMMAND 模式 & Active Object 模式

    COMMAND 模式 command模式非常简单,简单到你无法想象的地方. public interface Command { void execute(); } 这就是一个command模式的样子 ...

  5. Android开源库--ActiveAndroid(active record模式的ORM数据库框架)

    Github地址:https://github.com/pardom/ActiveAndroid 前言 我一般在Android开发中,几乎用不到SQLlite,因为一些小数据就直接使用Preferen ...

  6. Java多线程编程模式实战指南:Active Object模式(上)

    Active Object模式简介 Active Object模式是一种异步编程模式.它通过对方法的调用与方法的执行进行解耦来提高并发性.若以任务的概念来说,Active Object模式的核心则是它 ...

  7. java Active Object模式(上)

    Active Object模式简介 Active Object模式是一种异步编程模式.它通过对方法的调用与方法的执行进行解耦来提高并发性.若以任务的概念来说,Active Object模式的核心则是它 ...

  8. Java多线程编程模式实战指南一:Active Object模式(下)

    Active Object模式的评价与实现考量 Active Object模式通过将方法的调用与执行分离,实现了异步编程.有利于提高并发性,从而提高系统的吞吐率. Active Object模式还有个 ...

  9. Java多线程编程模式实战指南一:Active Object模式(上)

    Active Object模式简介 Active Object模式是一种异步编程模式.它通过对方法的调用与方法的执行进行解耦来提高并发性.若以任务的概念来说,Active Object模式的核心则是它 ...

随机推荐

  1. vue之组件通信

    vue组件通信一般分为以下几种情况: 1.父子组件通信: 2.兄弟组件通信: 3.跨多层级组件通信: 一.父子通信        父组件通过props传递数据给子组件,子组件通过emit发送事件传递数 ...

  2. solidworks 学习 (三)

    汽车轮毂三维建模

  3. BZOJ 4477: [Jsoi2015]字符串树 可持久化trie树

    这个是真——可持久化字典树..... code: #include <bits/stdc++.h> #define N 100006 #define setIO(s) freopen(s& ...

  4. AC自动机入门经典题目(两种表达方式)

    Keywords Search 指针方式: /* Keywords Search */ # include <iostream> # include <stdio.h> # i ...

  5. @Aspect注解并不属于@Component的一种

    也就是一个类单纯如果只添加了@Aspect注解,那么它并不能被context:component-scan标签扫描到. 想要被扫描到的话,需要追加一个@Component注解

  6. 微信小程序之页面导航栏

    效果图: 页面有点丑,作为初次学习,页面可以要求不那么美观,先学会再说.毕竟后面可以优化的很漂亮. 代码实例如下: <view class="section btn-area" ...

  7. ldap和phpldapadmin的安装部署

    LDAP 安装 一.安装LDAP 1. 安装包 yum install openssl-devel gcc libtool-ltdl-devel -y yum install openldap-ser ...

  8. 3ds Max学习日记(十一)——如何给模型上贴图

    参考链接:https://jingyan.baidu.com/article/e4511cf38a810b2b845eaf1f.html   之前一直都不知道怎么在3dsMax里给模型上材质和贴图,被 ...

  9. B2C电子商务系统研发——商品SKU分析和设计(一)

    一.SKU及相关概念定义 在设计商品SKU之前,首先让我们熟悉一下SKU和相关的一些概念. # 什么是SKU: SKU=Stock Keeping Unit(库存量单位) 同一型号的商品,或者说是同一 ...

  10. curl保留cookie

    curl -c cookie.txt http://localhost:9001/login.json?c=65CE13E16CF394D curl -b @cookie.txt http://loc ...