public MaptypeNum() throws InterruptedException { Map result = new HashMap<>(); // 参数任务上限 linkedBlockingQueue blockingQueue = new linkedBlockingQueue<>(1000); ThreadFactory threadFactory = new ThreadFactory() { // int i = 0; 用并发安全的包装类 AtomicInteger atomicInteger = new AtomicInteger(1); @Override public Thread newThread(Runnable r) { //创建线程 吧任务传进来 Thread thread = new Thread(r); // 给线程起个名字 thread.setName("MyThread" + atomicInteger.getAndIncrement()); return thread; } }; ThreadPoolExecutor pool = new ThreadPoolExecutor(5, 10, 1200L, TimeUnit.SECONDS, blockingQueue, threadFactory,new ThreadPoolExecutor.AbortPolicy()); pool.execute(new Runnable() { @Override public void run() { //查询全部数量 map.put("all",all); pool.shutdown(); } }); pool.execute(new Runnable() { @Override public void run() { //查询我发起的数量 map.put("started",started); pool.shutdown(); } }); pool.execute(new Runnable() { @Override public void run() { //查询已办数量 map.put("done",done); pool.shutdown(); } }); pool.execute(new Runnable() { @Override public void run() { //按类型查询数量 pool.shutdown(); } }); pool.execute(new Runnable() { @Override public void run() { //查询审批速度 pool.shutdown(); } }); //我的需求是等待所有线程任务都执行完毕后,统一返回结果,所以需要等待1分钟等所有线程执行完毕,1分钟还没执行完就抛异常 pool.awaitTermination(1, TimeUnit.MINUTES); result.put("lable",lable); result.putAll(map); return result; }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)