zoukankan      html  css  js  c++  java
  • 远程服务接口聚合带来的性能提升

    package 接口聚合;
    import java.util.concurrent.Callable;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.FutureTask;
    
    public class demo {
    
        public static ExecutorService executorService = Executors.newFixedThreadPool(5);
    
        public static void main(String[] args) {
            demo d = new demo();
            System.out.println(d.getABInfoOnWebOld());
            System.out.println(d.getABInfoOnWebNew());
    
    
        }
        //模拟WEB接口 ---接口串行
        public String getABInfoOnWebOld(){
            long before = System.currentTimeMillis();
            String s = RemoteService.serviceA();
            String s1 = RemoteService.serviceB();
            long cost = System.currentTimeMillis() - before;
            return s+":"+s1+"cost :"+cost;
        }
    
        //模拟WEB接口 ---接口集合
        public String getABInfoOnWebNew(){
            long before = System.currentTimeMillis();
            Callable<String> taskA = new Callable<String>() {
                public String call() throws Exception {
                    return RemoteService.serviceA();
                }
            };
            Callable<String> taskB = new Callable<String>() {
                public String call() throws Exception {
                    return RemoteService.serviceB();
                }
            };
            FutureTask futureTaskA = new FutureTask(taskA);
            FutureTask futureTaskB = new FutureTask(taskB);
            executorService.submit(futureTaskA);
            executorService.submit(futureTaskB);
            String result = "";
            try {
                result = futureTaskA.get() +":"+ futureTaskB.get();
                long cost = System.currentTimeMillis() - before;
                result = result+"cost:"+cost;
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                executorService.shutdown();
            }
            return result;
        }
        //模拟远程服务
        static class RemoteService{
    
            public static String serviceA(){
                try {
                    Thread.sleep(2000L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return "A";
            }
    
            public static String serviceB(){
                try {
                    Thread.sleep(3000L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return "B";
            }
    
        }
    
    }
  • 相关阅读:
    明年,我们依然年轻
    总有些东西会如台风一样的来
    ora00257错误处理方法
    ORACLE登录错误的解决方法
    C#中public new void add()的new在这里的意义
    Oracle Form Builder配置问题的一些总结
    作为程序员,你应该知道的职场晋升之路
    ORA01034错误的解决方法
    【转】JQUERY刷新页面
    【转】对C# 中堆栈,堆,值类型,引用类型的理解
  • 原文地址:https://www.cnblogs.com/brxHqs/p/10791302.html
Copyright © 2011-2022 走看看