当前位置: 首页 > news >正文

呼和浩特网站建设公司蜂鸟摄影网官网

呼和浩特网站建设公司,蜂鸟摄影网官网,高德地图在海外能用吗,河北关键词排名推广Active是主动的意思#xff0c;Active Object是主动对象的意思。主动对象就是拥有自己的独立线程。 Active Object模式不仅有自己的独立线程#xff0c;还可以接受异步消息#xff0c;并能返回处理结果。从标准的Active Objects设计入手#xff0c;将一个接口的方法调用转换…   Active是主动的意思Active Object是主动对象的意思。主动对象就是拥有自己的独立线程。 Active Object模式不仅有自己的独立线程还可以接受异步消息并能返回处理结果。从标准的Active Objects设计入手将一个接口的方法调用转换成可接收异步消息的主动对象也就是说方法的执行和方法的调用是在不同的线程中进行的接口方法的参数以及具体的实现封装成特定的Message告诉执行线程接口方法需要返回值必须以Future形式返回。 第一种方法当某个线程调用OrderService接口的findOrderDetails方法时是会发送一个包含findOrderDetails方法参数以及OrderService具体实现的Message到Message队列执行线程通过从队列中获取Message来调用具体的实现接口的方法的调用和接口方法的执行分别处于不同的线程中因此称该接口为Active Objects可接受异步消息的主动对象。 具体样例代码如下 public interface OrderService { FutureString findOrderDetails(long orderId); void order(String account,long orderId); } public class OrderServiceImpl implements OrderService{ Override public FutureString findOrderDetails(long orderId) { return FutureService.Long,StringnewService().submit(input-{ try { System.out.println(process the orderId-orderId); TimeUnit.SECONDS.sleep(10); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } return The order details Information; },orderId); }Override public void order(String account, long orderId) { try { System.out.println(process the orderId-orderId , account-account); TimeUnit.SECONDS.sleep(10); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } }} public class OrderServiceProxy implements OrderService{ private final OrderService orderService; private final ActiveMessageQueue activeMessageQueue;public OrderServiceProxy(OrderService orderService,ActiveMessageQueue activeMessageQueue) { this.orderServiceorderService; this.activeMessageQueueactiveMessageQueue; }Override public FutureString findOrderDetails(long orderId) { final ActiveFutureString activeFuturenew ActiveFuture(); MapString,Object paramsnew HashMap(); params.put(orderId, orderId); params.put(activeFuture, activeFuture); MethodMessage messagenew FindOrderDetailsMessage(params,orderService); activeMessageQueue.offer(message); return activeFuture; }Override public void order(String account, long orderId) { MapString,Object paramsnew HashMap(); params.put(account, account); params.put(orderId, orderId); MethodMessage messagenew OrderMessage(params,orderService); System.out.println(processing in OrderServicePoxy.order method); activeMessageQueue.offer(message); }} public class ActiveFutureT extends FutureTaskT{ Override public void finish(T result) { super.finish(result); } } import java.util.Map;public abstract class MethodMessage { protected final MapString,Object params; protected final OrderService orderService;public MethodMessage(MapString,Object params,OrderService orderService) { this.paramsparams; this.orderServiceorderService; }public abstract void execute(); } public class FindOrderDetailsMessage extends MethodMessage{public FindOrderDetailsMessage(MapString, Object params, OrderService orderService) { super(params, orderService); }Override public void execute() { FutureString realFutureorderService.findOrderDetails((Long) params.get(orderId)); ActiveFutureString activeFuture(ActiveFutureString)params.get(activeFuture); try { String resultrealFuture.get(); activeFuture.finish(result); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } }} import java.util.Map;public class OrderMessage extends MethodMessage{public OrderMessage(MapString, Object params, OrderService orderService) { super(params, orderService); }Override public void execute() { String account(String)params.get(account); long orderId(long)params.get(orderId); orderService.order(account, orderId); }} import java.util.LinkedList;public class ActiveMessageQueue { private final LinkedListMethodMessage messagenew LinkedList();public ActiveMessageQueue() { System.out.println(active Object Thread is build); new ActiveDaemonThread(this).start(); }public void offer(MethodMessage methodMessage) { synchronized(this) { message.add(methodMessage); System.out.println(processing in ActiveMessageQueue.offer method); this.notify(); } }protected MethodMessage take() { synchronized(this) { while(message.isEmpty()) { try { this.wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } System.out.println(processing in ActiveMessageQueue.take method); return message.removeFirst(); } } } public class ActiveDaemonThread extends Thread{ private final ActiveMessageQueue queue;public ActiveDaemonThread(ActiveMessageQueue queue) { super(ActiveDaemonThread); this.queuequeue; this.setDaemon(true); }Override public void run() { for(;;) { System.out.println( active daemon thread is running); MethodMessage methodMessagethis.queue.take(); methodMessage.execute(); } }} public class OrderServiceFactory { private final static ActiveMessageQueue activeMessageQueuenew ActiveMessageQueue();private OrderServiceFactory() {}public static OrderService toActiveObject(OrderService orderService) { return new OrderServiceProxy(orderService,activeMessageQueue); }} public class AOtest {public static void main(String[] args) { OrderService orderServiceOrderServiceFactory.toActiveObject(new OrderServiceImpl()); orderService.order(aACC, 5); FutureString forderService.findOrderDetails(50); try { System.out.println(future result is f.get()); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(Return immedately); try { Thread.currentThread().join(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } }} 第二种方法第一种方法在接口方法非常多的情况下会需要封装成很多的Message类。基于JDK动态代理的方式可以实现一种更加通用的Active Objects。这种方式下可以将任意接口方法转换w Active Objects如果接口方法有返回值必须返回Future类型才可以否则会抛出IllegalActiveMethod异常。示例代码如下 public class IllegalActivedException extends Exception{ public IllegalActivedException(String message) { super(message); } } public interface OrderService { FutureString findOrderDetails(long orderId); void order(String account,long orderId); } public class OrderServiceImpl implements OrderService{ActiveMethod Override public FutureString findOrderDetails(long orderId) { return FutureService.Long,StringnewService().submit(input-{ try { System.out.println(process the orderId-orderId); TimeUnit.SECONDS.sleep(10); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } return The order details Information; },orderId); }ActiveMethod Override public void order(String account, long orderId) { try { System.out.println(process the orderId-orderId , account-account); TimeUnit.SECONDS.sleep(10); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } }} import static java.lang.annotation.ElementType.METHOD; import static java.lang.annotation.RetentionPolicy.RUNTIME;import java.lang.annotation.Retention; import java.lang.annotation.Target;Retention(RUNTIME) Target(METHOD) public interface ActiveMethod {} public class ActiveMessage { private final Object[] objects; private final Method method; private final ActiveFutureObject future; private final Object service;private ActiveMessage(Builder builder) { this.objectsbuilder.objects; this.methodbuilder.method; this.futurebuilder.future; this.servicebuilder.service; }public void execute() { Object result; try { result method.invoke(service, objects); if(future!null) { Future? realFuture (Future?)result; Object realResultrealFuture.get(); future.finish(realResult); } } catch (Exception e) { if(future!null) { future.finish(null); } e.printStackTrace(); } }static class Builder{ private Object[] objects; private Method method; private ActiveFutureObject future; private Object service;public Builder useMethod(Method method) { this.methodmethod; return this; }public Builder returnFuture(ActiveFutureObject future) { this.futurefuture; return this; }public Builder withObjects(Object[] objects) { this.objectsobjects; return this; }public Builder forService(Object service) { this.serviceservice; return this; }public ActiveMessage build() { return new ActiveMessage(this); }}} import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy;import org.multithread.future.Future;public class ActiveServiceFactory { private final static ActiveMessageQueue queuenew ActiveMessageQueue();public static T T active(T instance) { Object proxyProxy.newProxyInstance(instance.getClass().getClassLoader(), instance.getClass().getInterfaces(), new ActiveInvocationHandler(instance)); return (T)proxy; }private static class ActiveInvocationHandlerT implements InvocationHandler{ private final T instance;ActiveInvocationHandler(T instance){ this.instanceinstance; }private void checkMethod(Method method) throws IllegalActivedException{ if(!isReturnVoidType(method)!isReturnFutureType(method)) { throw new IllegalActivedException(the method [method.getName()] return type must be void/Future); } }private boolean isReturnVoidType(Method method) { return method.getReturnType().equals(Void.TYPE); }private boolean isReturnFutureType(Method method) { return method.getReturnType().isAssignableFrom(Future.class); }Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if(method.isAnnotationPresent(ActiveMethod.class)) { this.checkMethod(method); ActiveMessage.Builder buildernew ActiveMessage.Builder(); builder.useMethod(method).withObjects(args).forService(instance); Object resultnull; if(this.isReturnFutureType(method)) { resultnew ActiveFuture(); builder.returnFuture((ActiveFuture) result); } queue.offer(builder.build()); return result; }else { return method.invoke(instance, args); } }}} import java.util.LinkedList;public class ActiveMessageQueue { private final LinkedListActiveMessage activeMessagesnew LinkedList();public ActiveMessageQueue() { System.out.println(active Object Thread is build); new ActiveDaemonThread(this).start(); }public void offer(ActiveMessage activeMessage) { synchronized(this) { this.activeMessages.add(activeMessage); System.out.println(processing in ActiveMessageQueue.offer method); this.notify(); } }public ActiveMessage takeActive() { synchronized(this) { while(this.activeMessages.isEmpty()) { try { this.wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return this.activeMessages.removeFirst(); } }} public class ActiveDaemonThread extends Thread{ private final ActiveMessageQueue queue;public ActiveDaemonThread(ActiveMessageQueue queue) { super(ActiveDaemonThread); this.queuequeue; this.setDaemon(true); }Override public void run() { for(;;) { System.out.println( active daemon thread is running); ActiveMessage activeMessagethis.queue.takeActive(); activeMessage.execute(); } } } public class AOtest {public static void main(String[] args) { ActiveServiceFactory activeInstancenew ActiveServiceFactory(); OrderService orderServiceactiveInstance.active(new OrderServiceImpl()); orderService.order(aACC, 5); FutureString forderService.findOrderDetails(150); try { System.out.println(future result is f.get()); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(Return immedately); try { Thread.currentThread().join(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } }}
http://www.zqtcl.cn/news/163039/

相关文章:

  • 网站建设的内容是什么在线阅读小说网站怎么建设
  • 福州网站开发哪家比较好建设网站需要掌握什么编程语言
  • 邹平做网站的公司莱芜人才网莱芜招聘
  • 旅行网站开发意义怎样优化网络速度
  • 手机微网站建设多少钱拟定网络设计方案
  • 厦门制作公司网站安卓原生app开发工具
  • worldpress英文网站建设wordpress输出外部文章
  • u9u8网站建设商业公司的域名
  • 有学给宝宝做衣服的网站吗防网站黑客
  • 十大搜索引擎网站微信小程序有什么用处?
  • 团购网站 seo烟台网站建设方案优化
  • 公司网站建设招标文件范本公益永久免费主机
  • 建设银行网站查询企业年金五合一免费建站
  • 做网站开发挣钱吗做网站手机版
  • 网站建设案例精粹 电子书广州白云学校网站建设
  • 良品铺子网站制作用什么软件来做网站
  • ip直接访问网站 备案哪有深圳设计公司
  • 平面构成作品网站第一设计
  • 济南小程序开发多少钱网站移动端优化工具
  • 大连开发区网站淘宝网站优化实例
  • 张家港建网站的公司做网站犯法了 程序员有责任吗
  • 小型企业网站建设项目浦东新区网站推广公司
  • 上海做网站优化公司ps最好用的素材网站
  • 网站建设品牌推广seo制作公司网站
  • 个人网站服务器一年多少钱科技让生活更美好作文450字
  • 开学第一课汉字做网站网盘资源搜索神器
  • 备案网站应用服务树莓派用来做网站
  • 找装修公司上什么网站湘潭交通网站
  • php网站服务建设网站增加关键字
  • 免费视频网站制作泰州东方医院