免费购物商城网站建设,网站后台验证码无法显示,外贸网站平台都有哪些,深圳品牌内衣t台秀在最近的联网电商项目中#xff0c;想要实现单个web容器内的支付请求#xff0c;全部能够统一宏观上同步#xff0c;实现粒度上面异步#xff0c;找来找去#xff0c;没有相关方面的框架或者方案#xff0c;于是最后基于现实场景#xff0c;打算使用Java callback 的多线…在最近的联网电商项目中想要实现单个web容器内的支付请求全部能够统一宏观上同步实现粒度上面异步找来找去没有相关方面的框架或者方案于是最后基于现实场景打算使用Java callback 的多线程方式假象伪并发量为100000/**** author gyx**/public class A{private double id 0;public A(double id){this.setId(id);}public double getId(){return id;}public void setId(double id){this.id id;}/*** 线程类* author gyx**/public class MyCallable implements Callable{private A obj;/*** 线程类构造函数传入线程序号* param taskNum*/public MyCallable(A obj){this.setObj(obj);}/*** 重写接口的方法子线程调用 * 此方法是生成0-99的数字(百位表示子线程序号)用list返回*/public A call() throws Exception{Date dateTmp1 new Date();System.out.println(business_start.............and------begin this.getObj().getId());this.getObj().setId(Math.random() * 1000000000);Date dateTmp2 new Date();long time dateTmp2.getTime() - dateTmp1.getTime();System.out.println(线程 Thread.currentThread().getId() 任务时间【 time 毫秒】);return this.getObj();}public A getObj(){return obj;}public void setObj(A obj){this.obj obj;}/*** author gyx*主线程调用*/public class Test{public static void main(String[] args) throws InterruptedException, ExecutionException{try{System.out.println(----程序开始运行----);Date date1 new Date();int taskSize 100000;// 创建一个线程池// ExecutorService pool Executors.newFixedThreadPool(4);ExecutorService pool Executors.newCachedThreadPool();// 创建多个有返回值的任务List list new ArrayList();for (int i 0; i taskSize; i){System.out.println(the current is ............ i);Callable c new MyCallable(new A(0));// 执行任务并获取Future对象Future f pool.submit(c);System.out.println(get-result---------------------- f.get().getId());list.add(f);}// 关闭线程池pool.shutdown();Date date2 new Date();System.out.println(----程序结束运行----程序运行时间【 (date2.getTime() - date1.getTime()) 毫秒】);// 获取所有并发任务的运行结果// for (Future f : list)// {// // 从Future对象上获取任务的返回值并输出到控制台// System.out.println(get-result---------------------- f.get().getId());// }}catch(Exception e){e.printStackTrace();System.exit(0);}finally{System.exit(0);}}}结果为----程序结束运行----程序运行时间【26351毫秒】调整线程池ExecutorService pool Executors.newFixedThreadPool(2);结果为----程序结束运行----程序运行时间【17128毫秒】调整线程池ExecutorService pool Executors.newFixedThreadPool(1);结果为程序运行时间【16269毫秒】结论newCachedThreadPool 效率最差线程数小于cpuh核心 2/1 效率相对高代价也比较平稳处理当下假想基本满足要求。