JAVA difference between the fence and atresia

  Atresia: a synchronization aid before completing a set of operations being performed in other threads, which allows one or more threads have been waiting for. That is, a group of threads waiting for a certain event occurs before the event did not happen, all the threads will be blocked waiting for; but after the incident, all the threads will begin; lockout initially in a closed state, when the incident lockout will be open, once open , lockout will always be open.

  Blocking CountDownLatchonly constructor method CountDownLatch(int count), when calling to the latch countDown()when the method of locking the counter is decremented by one, the counter is 0 when the latch, the latch opens, all threads will be started by a latching.

  Fence: A synchronization aid that allows a set of threads waiting for each other to reach a common barrier point. The use of the fence, you can make another thread to wait until all threads have reached a certain point, and then will open the fence, all threads will continue through the fence. CyclicBarrier support an optional  Runnable parameter , when the thread through the fence, runnable object will be called. Constructors, when the thread CyclicBarrier invoked on the objectwhen the method, the counter increments the fence, when the counter is, the fence is opened.CyclicBarrier(int parties, Runnable barrierAction)await()parties

  Difference: locking for all threads to wait for an external event occurs; fence is all the threads wait for each other, until all threads have reached a point when the open fence, then the thread can continue.

Lockout Example:

  There are five people, a referee. Meanwhile the five men ran, the referee starts counting, five people are to the end, the referee ordered a halt, then statistics from the beginning of the five men went to the last one crossing the finish line with how long.

import java.util.concurrent.CountDownLatch;

public class Race {

    public static void main(String[] args) {
        final int num = 5;
        final CountDownLatch begin = new CountDownLatch(1);
        final CountDownLatch end = new CountDownLatch(num);

        for (int i = 0; i < num; i++) {
            new Thread(new AWorker(i, begin, end)).start();
        }

        // judge prepare...
        try {
            Thread.sleep((long) (Math.random() * 5000));
        } catch (InterruptedException e1) {
            e1.printStackTrace();
        }

        System.out.println("judge say : run !");
        begin.countDown();
        long startTime = System.currentTimeMillis();

        try {
            end.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            long endTime = System.currentTimeMillis();
            System.out.println("judge say : all arrived !");
            System.out.println("spend time: " + (endTime - startTime));
        }

    }
}
class AWorker implements Runnable {
    final CountDownLatch begin;
    final CountDownLatch end;
    final int id;

    public AWorker(final int id, final CountDownLatch begin,
                   final CountDownLatch end) {
        this.id = id;
        this.begin = begin;
        this.end = end;
    }

    @Override
    public void run() {
        try {
            System.out.println(this.id + " ready !");
            begin.await();
            // run...
            Thread.sleep((long) (Math.random() * 10000));
        } catch (Throwable e) {
            e.printStackTrace();
        } finally {
            System.out.println(this.id + " arrived !");
            end.countDown();
        }
    }

}

Fence example:

  Or five men (five men really boring ..), this did not referee. As long as the provisions of five people have come to the end, and we can drink beer. However, as long as there is not a man to the finish, do not drink. There is also no requirement at the same time we have to start (of course also possible, plus latch).

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class Beer {

    public static void main(String[] args) {
        final int count = 5;
        final CyclicBarrier barrier = new CyclicBarrier(count, new Runnable() {
            @Override
            public void run() {
                System.out.println("drink beer!");
            }
        });

        // they do not have to start at the same time...
        for (int i = 0; i < count; i++) {
            new Thread(new Worker(i, barrier)).start();
        }
    }

}

class Worker implements Runnable {
    final int id;
    final CyclicBarrier barrier;

    public Worker(final int id, final CyclicBarrier barrier) {
        this.id = id;
        this.barrier = barrier;
    }

    @Override
    public void run() {
        try {
            System.out.println(this.id + "starts to run !");
            Thread.sleep((long) (Math.random() * 10000));
            System.out.println(this.id + "arrived !");
            this.barrier.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            e.printStackTrace();
        }
    }
}

Guess you like

Origin www.cnblogs.com/jing99/p/11318812.html