Java 多线程的一个小例子

Java 多线程的一个小例子,保存结果到StringBuffer:

 

    /**
     * 
     * @date:   Apr 21, 2021 5:16:38 PM
     * @author Henry Li
     * @throws ExecutionException 
     * @throws InterruptedException 
     */
    @Test
    public  void testMutilpeThread() throws InterruptedException, ExecutionException {
    	
    	String[] array = {"410038541200161302015","310038541200161302025","110038541200161302045","910038541200161302055","510038541200161303015","410038541200161303025","210038541200161303045","910038541200161303065","610038541200161304015","510038541200161304025","410038541200161304035","310038541200161304045","210038541200161304055","110038541200161304065","710038541200161305015","610038541200161305025","510038541200161305035","410038541200161305045","310038541200161305055","210038541200161305065","710038541200161306025","610038541200161306035","310038541200161301015","210038541200161301025","910038541200161301045","810038541200161301055","510038541200161306045","410038541200161306055","310038541200161306065","110038541200161301035","710038541200161301065","210038541200161302035","810038541200161302065","310038541200161303035","110038541200161303055","810038541200161306015"};
    	List<String> entries =  java.util.Arrays.asList(array);

    	ExecutorService service = Executors.newFixedThreadPool(entries.size());
    	
        List<CompletableFuture<String>> completableFutures = entries.stream()
                .map((entry) -> {
                            return CompletableFuture.supplyAsync(() -> {
                                try {
                                	System.out.println(Thread.currentThread().getId()+ ": begin " + new Date() );
                                    Thread.sleep(new Random().nextInt(5000) + 500);
                                	System.out.println(Thread.currentThread().getId()+ ": end " + new Date() +" " + entry );
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                                return Thread.currentThread().getId()+ " finished " + entry +" : " + new Date()+"; \r\n";
                            }, service);
                        }
                ).collect(Collectors.toList());

        //线程的执行结果
        StringBuffer sb = new StringBuffer();
        //线程聚合等待        
        CompletableFuture
                .allOf(completableFutures.toArray(new CompletableFuture[completableFutures.size()]))
                .thenApply((v) -> completableFutures.stream().map((cf) -> cf.join()))
                .get()
                .forEach(sb::append);
        //关闭线程池
        service.shutdown();
        assertNotNull(sb);
    	
    }

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值