多线程测试用例等待打印线程中结果

@Test
    public void executeTeste() {
        
        int page = (100 + 10)/10;
        StringBuffer st = new StringBuffer();
        final CountDownLatch countDown = new CountDownLatch(page);
        for (int i = 0; i <= page; i++) {
            
            executorService.execute(new Num(i, st));
        }
        
        try
        {
            System.err.println(st);
            countDown.await();
        }
        catch (InterruptedException e)
        {
            
        }

    }
    
    class Num implements Runnable{
        
        private int i;
        
        private StringBuffer st;
        
        public Num(int i, StringBuffer st) {
            this.i = i;
            this.st = st;
        }
        
        @Override
        public void run() {
            if(i % 2 == 0) {
                st.append(i);
            }
        }
        
    }

发布了52 篇原创文章 · 获赞 16 · 访问量 67万+

猜你喜欢

转载自blog.csdn.net/qq_30920479/article/details/103354443