Wie ist CountDownLatch verwendet, die in Java Multithreading?

Kann mir jemand helfen zu verstehen, was Java CountDownLatch ist und Wann es zu benutzen?

Ich don T haben eine sehr klare Vorstellung davon, wie dieses Programm funktioniert. So wie ich das verstehe alle drei threads starten gleichzeitig und jeder Thread aufrufen, CountDownLatch nach 3000ms. So der count down wird Dekrementieren. Nach der Verriegelung ist gleich null, das Programm druckt "Abgeschlossen". Vielleicht ist der Weg, habe ich verstanden, ist falsch.

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

class Processor implements Runnable {
    private CountDownLatch latch;

    public Processor(CountDownLatch latch) {
        this.latch = latch;
    }

    public void run() {
        System.out.println("Started.");

        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        latch.countDown();
    }
}

//-----------------------------------------------------

public class App {

    public static void main(String[] args) {

        CountDownLatch latch = new CountDownLatch(3); //coundown from 3 to 0

        ExecutorService executor = Executors.newFixedThreadPool(3); //3 Threads in pool

        for(int i=0; i < 3; i++) {
            executor.submit(new Processor(latch)); //ref to latch. each time call new Processes latch will count down by 1
        }

        try {
            latch.await();  //wait until latch counted down to 0
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("Completed.");
    }

}

InformationsquelleAutor der Frage amal | 2013-07-24

Schreibe einen Kommentar