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";
            }
    
        }
    
    }
  • 相关阅读:
    Item2:建造者替代多参数构造器
    Java常量赋值失败?
    0828 列表 增删改查
    字符 列表的切片规则
    0820 字符转换为数字
    使用 in 判断是否有敏感词
    while循环
    for循环
    isalnum 判断变量是否由字符或者数字组成
    使用lower upper等字符大小写指令选择为大小写单词转换大小写
  • 原文地址:https://www.cnblogs.com/brxHqs/p/10791302.html
Copyright © 2011-2022 走看看