博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【代码积累-1】ActiveObject
阅读量:4098 次
发布时间:2019-05-25

本文共 4069 字,大约阅读时间需要 13 分钟。

import java.util.concurrent.Callable;import java.util.concurrent.ExecutionException;import java.util.concurrent.Future;import java.util.concurrent.FutureTask;import java.util.concurrent.LinkedBlockingQueue;/*通过一个简单的例子研究一个典型的ActiveObject的实现*/public class ActiveObject {	/*为了方便,将所有相关的类都定义在一个class内*/	public void startTest() {		ServiceProxy proxy = new ServiceProxy();		Client client = new Client(proxy);				client.getNumberOfCustomers();		client.getBalance();	}		/*模拟客户端,此客户端调用proxy的异步方法,并保存Future对象,在晚些时候通过Future获取调用结果.	 * */	public class Client {		private ServiceProxy proxy = null;		private Future
getNumberOfCustomers = null; private Future
getBalance = null; public Client(ServiceProxy proxy) { super(); this.proxy = proxy; } public void getNumberOfCustomers() { getNumberOfCustomers = proxy.getNumberOfCustomers(); try { System.out.println("Number of customers = "+getNumberOfCustomers.get()); } catch (InterruptedException | ExecutionException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void getBalance() { getBalance = proxy.getBalance(); try { System.out.println("Balance = "+getBalance.get()); } catch (InterruptedException | ExecutionException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } /*对client提供业务调用接口,返回Future给client,内部使用Scheduler维护methodRequest*/ public class ServiceProxy { private Scheduler scheduler = new Scheduler(); private Servant servant = new Servant(); /*将这个servant封装到MethodRequest中,实际使用Servant必然需要一个继承体系,不同的业务使用不同的Servant*/ public Future
getNumberOfCustomers() { /*生成一个MethodRequest对象,并通过scheduler加入队列。*/ MethodRequestGetCustomerNumber request = new MethodRequestGetCustomerNumber(servant); return (Future
)scheduler.enqueue(request); } public Future
getBalance() { MethodRequestGetBalance request = new MethodRequestGetBalance(servant); return (Future
)scheduler.enqueue(request); } } /*封装客户端的调用*/ public abstract class MethodRequestInteger implements Callable
{ protected int requestType = 0; } public abstract class MethodRequestDouble implements Callable
{ protected int requestType = 0; } public class MethodRequestGetCustomerNumber extends MethodRequestInteger { private Servant servant = null; public MethodRequestGetCustomerNumber(Servant servant) { super(); this.servant = servant; } @Override public Integer call() throws Exception { // TODO Auto-generated method stub return servant.getNumberOfCustomers(); } } public class MethodRequestGetBalance extends MethodRequestDouble { private Servant servant = null; public MethodRequestGetBalance(Servant servant) { super(); this.servant = servant; } @Override public Double call() throws Exception { // TODO Auto-generated method stub return servant.getBalance(); } } /*维护请求队列,并调用队列中的methodRequest*/ public class Scheduler { private LinkedBlockingQueue
requestQueue = new LinkedBlockingQueue
(100); private Dispatcher dispatcher = new Dispatcher(); public Scheduler() { /*启动dispatcher线程(consumer),实际使用中,dispatcher应该会有多个,并使用线程池进行管理,此处简化为1个。*/ new Thread(dispatcher).start(); } /*对外提供的接口,client通过ServiceProxy可并发调用此方法*/ public Future
enqueue(Callable request) { final FutureTask task = new FutureTask(request); try { requestQueue.put(task); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } return task; } private class Dispatcher implements Runnable { public volatile boolean isRunning = true; @Override public void run() { // TODO Auto-generated method stub while(!Thread.currentThread().isInterrupted() && true == isRunning) { try { FutureTask task = requestQueue.take(); if( null != task ) { task.run(); /*task利用给定的Servant去执行调用,这里引入了Command模式*/ } } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); Thread.currentThread().interrupt(); /*阻塞状态下被中断,中断标志会清掉,需要重新置位一下*/ } } } } } /*模拟执行最终的调用*/ public class Servant { public int getNumberOfCustomers() { return 11; } public double getBalance() { return 0.177; } }}

转载地址:http://pvhii.baihongyu.com/

你可能感兴趣的文章
Spring MVC中使用Thymeleaf模板引擎
查看>>
Spring Boot构建简单的微博应用
查看>>
Spring处理表单提交
查看>>
Spring MVC异常处理
查看>>
Leetcode 1180. Count Substrings with Only One Distinct Letter [Python]
查看>>
PHP 7 的五大新特性
查看>>
php使用 memcache 来存储 session
查看>>
php实现socket(转)
查看>>
PHP底层的运行机制与原理
查看>>
深入了解php底层机制
查看>>
PHP中的stdClass 【转】
查看>>
XHProf-php轻量级的性能分析工具
查看>>
PHP7新特性 What will be in PHP 7/PHPNG
查看>>
比较strtr, str_replace和preg_replace三个函数的效率
查看>>
ubuntu 下编译PHP5.5.7问题:configure: error: freetype.h not found.
查看>>
PHP编译configure时常见错误 debian centos
查看>>
configure: error: Please reinstall the BZip2 distribution
查看>>
OpenCV gpu模块样例注释:video_reader.cpp
查看>>
【增强学习在无人驾驶中的应用】
查看>>
《python+opencv实践》四、图像特征提取与描述——29理解图像特征
查看>>