Java 同步工具类

Java 同步工具类

CountDownLatch

一种同步辅助类,允许一个或多个线程等待,知道在其他线程中执行的操作完成

package com.wdg.concurrent;

import java.util.concurrent.CountDownLatch;

/**
 * @description: CountDownLatch实例
 * @author: WuDG/1490727316@qq.com
 * @date: 2021/3/17 15:09
 */
public class CountDownLatchDemo {
    public static void main(String[] args) throws InterruptedException {
        // 类似计数器,countDownLatch.countDown()执行了6此后才会继续执行
        CountDownLatch countDownLatch = new CountDownLatch(6);

        for (int i = 0; i < 9; i++) {
            final int tmp = i;
            new Thread(() -> {
                System.out.println("线程:"+Thread.currentThread().getName()+",i="+tmp);
                countDownLatch.countDown();
            }).start();
        }
        countDownLatch.await();
        System.out.println("执行结束");
    }
}

CyclicBarrier

类似上面的CountDownLatch类,CountDownLatch是做减法计数器,而CyclicBarrier是做加法计数器。达到某个给定的计数后触发执行

package com.wdg.concurrent;

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

/**
 * @description: CyclicBarrier实例
 * @author: WuDG/1490727316@qq.com
 * @date: 2021/3/17 15:09
 */
public class CyclicBarrierDemo {
    public static void main(String[] args) {
        // 计数资源,达到6个以后才会执行CyclicBarrier构造方法中的Runnable实现类
        CyclicBarrier cyclicBarrier = new CyclicBarrier(6,() -> System.out.println("执行开始"));

        for (int i = 0; i < 9; i++) {
            final int tmp = i;
            new Thread(() -> {
                System.out.println("线程:"+Thread.currentThread().getName()+",i="+tmp);
                try {
                    cyclicBarrier.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
            }).start();
        }
    }
}

Semaphore

计数信号量。设定一个临界资源,后续的线程要想进入(acquire)必须等待前面的线程释放(release)

package com.wdg.concurrent;

import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

/**
 * @description: Semaphore实例
 * @author: WuDG/1490727316@qq.com
 * @date: 2021/3/17 15:09
 */
public class SemaphoreDemo {
    public static void main(String[] args) {
        // 临界资源,如3个车位,第4个线程想要进入则需要等待先进入的退出后才允许进入
        Semaphore semaphore = new Semaphore(3);

        for (int i = 0; i < 9; i++) {
            final int tmp = i;
            new Thread(() -> {
                try {
                    semaphore.acquire();
                    System.out.println("线程:"+Thread.currentThread().getName()+"开始执行,i="+tmp);
                    // 线程持有资源时间3s
                    TimeUnit.SECONDS.sleep(3);
                    System.out.println("线程:"+Thread.currentThread().getName()+"执行结束,i="+tmp);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    semaphore.release();
                }
            }).start();
        }
    }
}
# java  并发 

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×