Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
302 views
in Technique[技术] by (71.8m points)

multithreading - How to run a task once every Threads finished running in Java?

I have a loop which create a new Thread on each iteration, like so:

for(int i = 0; i < REPEAT; i++) {
    new Thread(new MyTask(i)).start();
    Thread.sleep(1);
}

private void finalTask() {
    //Some code to be executed once every threads stopped running
}

Where MyTask is a class implementing Runnable. My goal is: I would like to run finalTask once every threads stopped. To achieve this, I have tried incrementing a variable by 1 each time a thread finished running, and once this variable was equal to REPEAT, the final task would run. But this didn't work. I've searched on Google and StackOverlow for answers to my problem, but there are very little informations on this and none of them worked as well. There would always be a thread that was running after the final task. How can I do this then?

question from:https://stackoverflow.com/questions/65859885/how-to-run-a-task-once-every-threads-finished-running-in-java

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use a CountDownLatch for this. A CountDownLatch is

A synchronization aid that allows one or more threads to wait until a set of operations being performed in other threads completes.

CountDownLatch countDownLatch = new CountDownLatch(REPEAT);
for (int i = 0; i < REPEAT; i++) {
    new Thread(new MyTask(i, countDownLatch)).start();
    Thread.sleep(1);
}
finalTask(countDownLatch);

I create a CountDownLatch whose count is initialized to the value of REPEAT. I pass this to each of the threads and to the finalTask method.

Each thread after doing its work should call the countDown method of the countDownLatch.

private static class MyTask implements Runnable {

    private int i;
    private CountDownLatch countDownLatch;

    private MyTask(int i, CountDownLatch countDownLatch) {
        this.i = i;
        this.countDownLatch = countDownLatch;
    }

    @Override
    public void run() {
        //Perform some task
        System.out.println("Running " + i);
        countDownLatch.countDown();
    }
}

The first line of the finalTask method should call the await method of the CountDownLatch. This will cause the thread running the finalTask wait till the count of the CountDownLatch reaches 0 i.e., until all threads (REPEAT number of them) has completed and invoked the countDown of the CountDownLatch.

 private static void finalTask(CountDownLatch countDownLatch) {
    try {
        countDownLatch.await(); //this will wait until the count becomes 0.
    } catch (InterruptedException e) {
        e.printStackTrace(); //handle it appropriately
    }
    //Some code to be executed once all threads stopped running
    System.out.println("All done");
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...