wait until all threads finish their work in java

后端 未结 16 2168
情深已故
情深已故 2020-11-22 14:10

I\'m writing an application that has 5 threads that get some information from web simultaneously and fill 5 different fields in a buffer class.
I need to validate buffer

16条回答
  •  孤城傲影
    2020-11-22 14:58

    The existing answers said could join() each thread.

    But there are several ways to get the thread array / list:

    • Add the Thread into a list on creation.
    • Use ThreadGroup to manage the threads.

    Following code will use the ThreadGruop approach. It create a group first, then when create each thread specify the group in constructor, later could get the thread array via ThreadGroup.enumerate()


    Code

    SyncBlockLearn.java

    import org.testng.Assert;
    import org.testng.annotations.Test;
    
    /**
     * synchronized block - learn,
     *
     * @author eric
     * @date Apr 20, 2015 1:37:11 PM
     */
    public class SyncBlockLearn {
        private static final int TD_COUNT = 5; // thread count
        private static final int ROUND_PER_THREAD = 100; // round for each thread,
        private static final long INC_DELAY = 10; // delay of each increase,
    
        // sync block test,
        @Test
        public void syncBlockTest() throws InterruptedException {
            Counter ct = new Counter();
            ThreadGroup tg = new ThreadGroup("runner");
    
            for (int i = 0; i < TD_COUNT; i++) {
                new Thread(tg, ct, "t-" + i).start();
            }
    
            Thread[] tArr = new Thread[TD_COUNT];
            tg.enumerate(tArr); // get threads,
    
            // wait all runner to finish,
            for (Thread t : tArr) {
                t.join();
            }
    
            System.out.printf("\nfinal count: %d\n", ct.getCount());
            Assert.assertEquals(ct.getCount(), TD_COUNT * ROUND_PER_THREAD);
        }
    
        static class Counter implements Runnable {
            private final Object lkOn = new Object(); // the object to lock on,
            private int count = 0;
    
            @Override
            public void run() {
                System.out.printf("[%s] begin\n", Thread.currentThread().getName());
    
                for (int i = 0; i < ROUND_PER_THREAD; i++) {
                    synchronized (lkOn) {
                        System.out.printf("[%s] [%d] inc to: %d\n", Thread.currentThread().getName(), i, ++count);
                    }
                    try {
                        Thread.sleep(INC_DELAY); // wait a while,
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
    
                System.out.printf("[%s] end\n", Thread.currentThread().getName());
            }
    
            public int getCount() {
                return count;
            }
        }
    }
    

    The main thread will wait for all threads in the group to finish.

提交回复
热议问题