欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

多线程并发——闭锁,珊栏,信号量

程序员文章站 2022-05-02 12:30:01
...


闭锁(CountDownLatch):多线程并发的时候,设置计数器,当计数器的值没有减到0的时候,调用await()方法的线程会阻塞。

  • countDownLatch():用来计数,每执行一次,计数器的值减去1

  • await():用来阻塞当前线程,当计数器中的值没有减为0的时候,await()方法让当前线程阻塞

设置计数器线程:

import java.util.concurrent.CountDownLatch;

//设置计数器线程
public class Thread1 implements Runnable {
    private CountDownLatch countDownLatch;

    public Thread1(CountDownLatch countDownLatch) {
        this.countDownLatch = countDownLatch;
    }

    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println(Thread.currentThread().getName() + " : " + i);
            countDownLatch.countDown();
        }
    }
}

设置计数器阻塞线程:

import java.util.concurrent.CountDownLatch;

//当前要等待计数器的线程
public class Thread2 implements Runnable {

    private CountDownLatch countDownLatch;

    public Thread2(CountDownLatch countDownLatch) {
        this.countDownLatch = countDownLatch;
    }

    @Override
    public void run() {
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        for (int i = 0; i < 10; i++) {
            System.out.println(Thread.currentThread().getName() + " : " + i);
        }
    }
}

设置主线程:

import java.util.concurrent.CountDownLatch;

//设置计数器线程
public class Thread1 implements Runnable {
    private CountDownLatch countDownLatch;

    public Thread1(CountDownLatch countDownLatch) {
        this.countDownLatch = countDownLatch;
    }

    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println(Thread.currentThread().getName() + " : " + i);
            countDownLatch.countDown();
        }
    }
}

打印结果:这里可以看到Thread2线程被阻塞住了,因为此时计数器的值为1,就实现了多线程的同步

多线程并发——闭锁,珊栏,信号量



珊栏(CyclicBarrier):一种同步工具类,栅栏能阻塞一组线程直到某个事件发生,直到所有线程都到达栅栏点,栅栏才会打开。所以栅栏一般用于多个线程需要相互等待的情况。

  • await():用来阻塞线程,当没有达到预设数目时,此时所有在珊栏外的线程都会阻塞

设置测试线程:

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

//当前要等待计数器的线程
public class Thread2 implements Runnable {

    private CyclicBarrier cyclicBarrier;

    public Thread2(CyclicBarrier cyclicBarrier) {
        this.cyclicBarrier = cyclicBarrier;
    }

    @Override
    public void run() {
        try {
            cyclicBarrier.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + " : hello");
    }
}

设置主线程:

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;

public class CountDownTest {

    public static void main(String[] args) throws InterruptedException {
//        此时将计数器的值设置为11,而我们此时在Thread1中只是将计数器的值减少到1,所以Thread2中一定会阻塞
        CyclicBarrier cyclicBarrier = new CyclicBarrier(5);
        Thread thread1 = new Thread(new Thread2(cyclicBarrier));
        Thread thread2 = new Thread(new Thread2(cyclicBarrier));
        Thread thread3 = new Thread(new Thread2(cyclicBarrier));
        Thread thread4 = new Thread(new Thread2(cyclicBarrier));
        Thread thread5 = new Thread(new Thread2(cyclicBarrier));

        thread1.start();
        thread2.start();
        thread3.start();
        thread4.start();
        thread5.start();
    }
}

此时设置了珊栏的数目是5个线程,所以此时这5个线程可以并发执行,如果此时到达珊栏的线程数大于或着小于,那么线程就会阻塞。



信号量(Semaphore):java中使用Semaphore可以实现使用信号控制多线程并发。Semaphore可以控制某个资源可以同时被访问的次数。在Semaphore中,只有获取到了信号量的线程才可以执行,否则会阻塞,所以一定要记得执行完的线程要释放信号量,不然后面的线程无法获得信号量被阻塞住。

  • acquire():获取一个信号量
  • release():释放一个信号量

操作实现类:

import java.util.concurrent.Semaphore;

public class CountDownTest {

    public static void main(String[] args) throws InterruptedException {
//        此时将计数器的值设置为11,而我们此时在Thread1中只是将计数器的值减少到1,所以Thread2中一定会阻塞
        Semaphore semaphore = new Semaphore(5);

        System.out.println(semaphore.availablePermits());
        semaphore.acquire(6);
        System.out.println("sigsoia");
    }
}

执行结果:

多线程并发——闭锁,珊栏,信号量

只打印了5条信息,因为前5个获取了信号量,却没释放,所以剩下的线程必须阻塞

相关标签: SE