countdownlantch闭锁和cyclicbarrirer栅栏和semaphore信号量

35 阅读1分钟

image.png

image.png

image.png

image.png

 
public class Test {
	public static void main(String[] args) {
		final CountDownLatch latch = new CountDownLatch(2);// 2个线程协作
		new Thread() {
			public void run() {
				try {
					System.out.println("子线程" + Thread.currentThread().getName()
							+ "正在执行");
					Thread.sleep(3000);
					System.out.println("子线程" + Thread.currentThread().getName()
							+ "执行完毕");
					latch.countDown();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			};
		}.start();
		new Thread() {
			public void run() {
				try {
					System.out.println("子线程" + Thread.currentThread().getName()
							+ "正在执行");
					Thread.sleep(3000);
					System.out.println("子线程" + Thread.currentThread().getName()
							+ "执行完毕");
					latch.countDown();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			};
		}.start();
		try {
			System.out.println("等待2个子线程执行完毕…");
			latch.await();
			System.out.println("2个子线程已经执行完毕");
			System.out.println("继续执行主线程");
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}

image.png

image.png image.png

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class CyclicBarrierDemo {
    public static void main(String[] args) {
        ExecutorService service = Executors.newFixedThreadPool(5);
        final CyclicBarrier barrier = new CyclicBarrier(5);
        for (int i = 0; i < 5; i++) {
            service.execute(new Player("玩家" + i, barrier));
        }
        service.shutdown();
    }
}
Java

import java.util.Random;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.TimeUnit;
public class Player implements Runnable {
    private final String name;
    private final CyclicBarrier barrier;
    public Player(String name, CyclicBarrier barrier) {
        this.name = name;
        this.barrier = barrier;
    }
    public void run() {
        try {
            TimeUnit.SECONDS.sleep(1 + (new Random().nextInt(3)));
            System.out.println(name + “已准备,等待其他玩家准备…”);
            barrier.await();
            TimeUnit.SECONDS.sleep(1 + (new Random().nextInt(3)));
            System.out.println(name + “已加入游戏”);
        } catch (InterruptedException e) {
            System.out.println(name + “离开游戏”);
        } catch (BrokenBarrierException e) {
            System.out.println(name + “离开游戏”);
        }
    }
}

image.png

image.png

image.png

import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
public class TestSemaphore {
    private static final int THREAD_COUNT = 10;
    private static ExecutorService threadPool = Executors.newFixedThreadPool(THREAD_COUNT);
    private static Semaphore s = new Semaphore(5);
    public static void main(String[] args) {
        for (int i = 0; i < THREAD_COUNT; i++) {
            threadPool.execute(new Employee(String.valueOf(i), s));
        }
        threadPool.shutdown();
    }
}
Java

import java.util.Random;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
 
class Employee implements Runnable {
	private String id;
	private Semaphore semaphore;
	private static Random rand = new Random(47);
 
	public Employee(String id, Semaphore semaphore) {
		this.id = id;
		this.semaphore = semaphore;
	}
 
	public void run() {
		try {
			semaphore.acquire();
			System.out.println(this.id + " is using the toilet");
			TimeUnit.MILLISECONDS.sleep(rand.nextInt(2000));
			System.out.println(this.id + " is leaving");
			semaphore.release();
		} catch (InterruptedException e) {
		}
	}
}

image.png

image.png