实现的思路是,通过代理将方法的调用转变为向阻塞队列中添加一个请求,由一个线程取出请求后执行实际的方法,然后将结果设置到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()); }
}

最新文章

  1. swiper.animate~之~可以执行两种动画的升级版的Swiper Animate
  2. CallBack实践。
  3. UILabel添加图片之富文本的简单应用
  4. C#时间操作
  5. Volly框架的使用基础版及使用中的一些坑 Ace 网络篇(三)
  6. start apache2 failed in Ubuntu
  7. Z-BlogPHP 安装出现 (8) Undefined offset: 6 解决方法
  8. eclipse升级后Android使用JAR报错
  9. hbase列表排序
  10. Hibernate遇到oracle之主键生成策略
  11. 腾讯面试题:10G 个整数,乱序排列,要求找出中位数。内存限制为 2G。
  12. ansible离线安装
  13. dataframe基础
  14. 学以致用三十-----pycharm创建django项目忘记添加app
  15. 莫烦theano学习自修第八天【分类问题】
  16. Mac安装MySQL数据库
  17. 关于等效的物理意义 On the Physical Meaning of Equivalence
  18. Flex 程序执行顺序!
  19. Mybatis中#和$区别(带脑图)
  20. SPOJ 10628. Count on a tree (树上第k大,LCA+主席树)

热门文章

  1. lambda表达式格式以及应用场景?
  2. 求x,y中的最大值
  3. vant - Navbar slot 插槽使用
  4. c++处理字符串string.find()与string::npos
  5. BZOJ 1758: [Wc2010]重建计划 01分数规划+点分治+单调队列
  6. vue+elementUI完成注册及登陆
  7. 在WinDbg里使用MEX调试扩展
  8. 【JOISC2019|2019】【20190622】cake3
  9. (8)Go Map
  10. node.js切换多个版本