剑指JUC原理-18.同步协作

news2025/4/18 2:17:28
  • 👏作者简介:大家好,我是爱吃芝士的土豆倪,24届校招生Java选手,很高兴认识大家
  • 📕系列专栏:Spring源码、JUC源码
  • 🔥如果感觉博主的文章还不错的话,请👍三连支持👍一下博主哦
  • 🍂博主正在努力完成2023计划中:源码溯源,一探究竟
  • 📝联系方式:nhs19990716,加我进群,大家一起学习,一起进步,一起对抗互联网寒冬👀

文章目录

    • Semaphore
      • 基本使用
      • 限制对共享资源的使用
        • semaphore 实现
      • Semaphore 原理
        • 加锁解锁流程
    • CountdownLatch
      • 源码
      • 应用之同步等待多线程准备完毕
      • 应用之同步等待多个远程调用结束
    • CyclicBarrier

Semaphore

基本使用

[ˈsɛməˌfɔr] 信号量,用来限制能同时访问共享资源的线程上限。

public static void main(String[] args) {
        // 1. 创建 semaphore 对象
        Semaphore semaphore = new Semaphore(3);
        // 2. 10个线程同时运行
        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                // 3. 获取许可
                try {
                    semaphore.acquire();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                try {
                    log.debug("running...");
                    sleep(1);
                    log.debug("end...");
                } finally {
                    // 4. 释放许可
                    semaphore.release();
                }
            }).start();
        }
    }

输出

07:35:15.485 c.TestSemaphore [Thread-2] - running... 
07:35:15.485 c.TestSemaphore [Thread-1] - running... 
07:35:15.485 c.TestSemaphore [Thread-0] - running... 
07:35:16.490 c.TestSemaphore [Thread-2] - end... 
07:35:16.490 c.TestSemaphore [Thread-0] - end... 
07:35:16.490 c.TestSemaphore [Thread-1] - end... 
07:35:16.490 c.TestSemaphore [Thread-3] - running... 
07:35:16.490 c.TestSemaphore [Thread-5] - running... 
07:35:16.490 c.TestSemaphore [Thread-4] - running... 
07:35:17.490 c.TestSemaphore [Thread-5] - end... 
07:35:17.490 c.TestSemaphore [Thread-4] - end... 
07:35:17.490 c.TestSemaphore [Thread-3] - end... 
07:35:17.490 c.TestSemaphore [Thread-6] - running... 
07:35:17.490 c.TestSemaphore [Thread-7] - running... 
07:35:17.490 c.TestSemaphore [Thread-9] - running... 
07:35:18.491 c.TestSemaphore [Thread-6] - end... 
07:35:18.491 c.TestSemaphore [Thread-7] - end... 
07:35:18.491 c.TestSemaphore [Thread-9] - end... 
07:35:18.491 c.TestSemaphore [Thread-8] - running... 
07:35:19.492 c.TestSemaphore [Thread-8] - end... 

限制对共享资源的使用

semaphore 实现

使用 Semaphore 限流,在访问高峰期时,让请求线程阻塞,高峰期过去再释放许可,当然它只适合限制单机
线程数量,并且仅是限制线程数

用 Semaphore 实现简单连接池,对比『享元模式』下的实现(用wait notify),性能和可读性显然更好

Semaphore 实现简单连接池相对于使用"享元模式"(使用 wait 和 notify)的实现在性能和可读性方面更好的原因主要有以下几点:

简洁的接口:Semaphore 提供了 acquire 和 release 方法来获取和释放资源,这样可以更直观地控制资源的访问。而"享元模式"下的实现需要手动管理线程的等待和唤醒,使用 wait 和 notify 的机制更为复杂,可读性较差。

并发控制:Semaphore 可以灵活地控制并发线程的数量,通过控制许可证的数量来限制同时访问资源的线程数量。而"享元模式"中的 wait 和 notify 机制需要手动管理线程的等待和唤醒,容易出现死锁同步问题。

性能优化Semaphore 可以通过设置初始许可证数量、公平性等参数来进行性能优化,而"享元模式"中的 wait 和 notify 更多地依赖于程序员手动编写的同步逻辑,容易出现性能瓶颈和难以调试的问题

可维护性:Semaphore 提供了一个一致的、标准的接口,易于理解和维护。而"享元模式"下的实现需要程序员手动管理线程的等待和唤醒,代码复杂度高,可维护性差。

因此,Semaphore 实现简单连接池在性能和可读性上更优,它提供了更直观、简洁和安全的方式来管理并发访问资源。同时,Semaphore 对并发的控制更为灵活,使得整个连接池的管理更加高效和可靠。

class Pool {
    // 1. 连接池大小
    private final int poolSize;
    // 2. 连接对象数组
    private Connection[] connections;
    // 3. 连接状态数组 0 表示空闲, 1 表示繁忙
    private AtomicIntegerArray states;
    private Semaphore semaphore;
    // 4. 构造方法初始化
    public Pool(int poolSize) {
        this.poolSize = poolSize;
        // 让许可数与资源数一致
        this.semaphore = new Semaphore(poolSize);
        this.connections = new Connection[poolSize];
        this.states = new AtomicIntegerArray(new int[poolSize]);
        for (int i = 0; i < poolSize; i++) {
            connections[i] = new MockConnection("连接" + (i+1));
        }
    }
    // 5. 借连接
    public Connection borrow() {// t1, t2, t3
        // 获取许可
        try {
            semaphore.acquire(); // 没有许可的线程,在此等待
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        for (int i = 0; i < poolSize; i++) {
            // 获取空闲连接
            if(states.get(i) == 0) {
                if (states.compareAndSet(i, 0, 1)) {
                    log.debug("borrow {}", connections[i]);
                    return connections[i];
                }
            }
        }
        // 不会执行到这里
        return null;
    }
    // 6. 归还连接
    public void free(Connection conn) {
        for (int i = 0; i < poolSize; i++) {
            if (connections[i] == conn) {
                states.set(i, 0);
                log.debug("free {}", conn);
                semaphore.release();
                break;
            }
        }
    }
}

Semaphore 原理

加锁解锁流程

Semaphore 有点像一个停车场,permits 就好像停车位数量,当线程获得了 permits 就像是获得了停车位,然后
停车场显示空余车位减一

刚开始,permits(state)为 3,这时 5 个线程来获取资源

在这里插入图片描述

public Semaphore(int permits) {
        sync = new NonfairSync(permits);
    }

static final class NonfairSync extends Sync {
        private static final long serialVersionUID = -2694183684443567898L;

        NonfairSync(int permits) {
            super(permits);
        }

        protected int tryAcquireShared(int acquires) {
            return nonfairTryAcquireShared(acquires);
        }
    }

abstract static class Sync extends AbstractQueuedSynchronizer {
        private static final long serialVersionUID = 1192457210091910933L;

    // 核心在这里将state设置进来
        Sync(int permits) {
            setState(permits);
        }

        final int getPermits() {
            return getState();
        }

        final int nonfairTryAcquireShared(int acquires) {
            for (;;) {
                int available = getState();
                int remaining = available - acquires;
                if (remaining < 0 ||
                    compareAndSetState(available, remaining))
                    return remaining;
            }
        }

假设其中 Thread-1,Thread-2,Thread-4 cas 竞争成功,而 Thread-0 和 Thread-3 竞争失败,进入 AQS 队列
park 阻塞

以Thread1为例:

public void acquire() throws InterruptedException {
        sync.acquireSharedInterruptibly(1);
    }

public final void acquireSharedInterruptibly(int arg)
            throws InterruptedException {
        if (Thread.interrupted())
            throw new InterruptedException();
    // 然后又调用 tryAcquireShared
        if (tryAcquireShared(arg) < 0)
            doAcquireSharedInterruptibly(arg);
    }

protected int tryAcquireShared(int acquires) {
            for (;;) {
                if (hasQueuedPredecessors())
                    return -1;
                int available = getState();
                int remaining = available - acquires;
                if (remaining < 0 ||
                    compareAndSetState(available, remaining))
                    // 以我们当前的例子来说,其实是符合的,3-1 = 2,返回2 不小于0 所以加锁成功
                    // 同理 thread 1 2 3都是一样的
                    return remaining;
            }
        }

// 如果竞争失败了呢?
private void doAcquireSharedInterruptibly(int arg)
        throws InterruptedException {
    // 和前面一样的流程,先设置头结点
        final Node node = addWaiter(Node.SHARED);
        boolean failed = true;
        try {
            for (;;) {
                final Node p = node.predecessor();
                if (p == head) {
                    // 尝试再获取一次
                    int r = tryAcquireShared(arg);
                    if (r >= 0) {
                        setHeadAndPropagate(node, r);
                        p.next = null; // help GC
                        failed = false;
                        return;
                    }
                }
                // 然后走到这里来 park
                if (shouldParkAfterFailedAcquire(p, node) &&
                    parkAndCheckInterrupt())
                    throw new InterruptedException();
            }
        } finally {
            if (failed)
                cancelAcquire(node);
        }
    }

在这里插入图片描述

这时 Thread-4 释放了 permits,状态如下

public void release() {
        sync.releaseShared(1);
    }

public final boolean releaseShared(int arg) {
        if (tryReleaseShared(arg)) {
            // 释放了之后进入 doReleaseShared方法
            doReleaseShared();
            return true;
        }
        return false;
    }

protected final boolean tryReleaseShared(int releases) {
            for (;;) {
                int current = getState();
                int next = current + releases;
                if (next < current) // overflow
                    throw new Error("Maximum permit count exceeded");
                if (compareAndSetState(current, next))
                    return true;
            }
        }

private void doReleaseShared() {
        
   
        for (;;) {
            Node h = head;
            if (h != null && h != tail) {
                int ws = h.waitStatus;
                if (ws == Node.SIGNAL) {
                    if (!compareAndSetWaitStatus(h, Node.SIGNAL, 0))
                        continue;            // loop to recheck cases
                    // unpark唤醒
                    unparkSuccessor(h);
                }
                else if (ws == 0 &&
                         !compareAndSetWaitStatus(h, 0, Node.PROPAGATE))
                    continue;                // loop on failed CAS
            }
            if (h == head)                   // loop if head changed
                break;
        }
    }

// 唤醒了以后
private void doAcquireSharedInterruptibly(int arg)
        throws InterruptedException {
    // 和前面一样的流程,先设置头结点
        final Node node = addWaiter(Node.SHARED);
        boolean failed = true;
        try {
            for (;;) {
                final Node p = node.predecessor();
                if (p == head) {
                    // 尝试再获取一次
                    // 唤醒了之后,再次for循环就能到这里
                    int r = tryAcquireShared(arg);
                    if (r >= 0) {
                        setHeadAndPropagate(node, r);
                        p.next = null; // help GC
                        failed = false;
                        return;
                    }
                }
                // 然后走到这里来 park
                if (shouldParkAfterFailedAcquire(p, node) &&
                    parkAndCheckInterrupt())
                    throw new InterruptedException();
            }
        } finally {
            if (failed)
                cancelAcquire(node);
        }
    }


private void setHeadAndPropagate(Node node, int propagate) {
        Node h = head; // Record old head for check below
        setHead(node);
        
    // 唤醒了以后重新设置头结点即可
    
        if (propagate > 0 || h == null || h.waitStatus < 0 ||
            (h = head) == null || h.waitStatus < 0) {
            Node s = node.next;
            if (s == null || s.isShared())
                doReleaseShared();
        }
    }

在这里插入图片描述

接下来 Thread-0 竞争成功,permits 再次设置为 0,设置自己为 head 节点,断开原来的 head 节点,unpark 接
下来的 Thread-3 节点,但由于 permits 是 0,因此 Thread-3 在尝试不成功后再次进入 park 状态

在这里插入图片描述

CountdownLatch

用来进行线程同步协作,等待所有线程完成倒计时。

其中构造参数用来初始化等待计数值,await() 用来等待计数归零(等待归零,只有归零了才能继续运行),countDown() 用来让计数减一

public static void main(String[] args) throws InterruptedException {
        CountDownLatch latch = new CountDownLatch(3);
        new Thread(() -> {
            log.debug("begin...");
            sleep(1);
            latch.countDown();
            log.debug("end...{}", latch.getCount());
        }).start();
        new Thread(() -> {
            log.debug("begin...");
            sleep(2);
            latch.countDown();
            log.debug("end...{}", latch.getCount());
        }).start();
        new Thread(() -> {
            log.debug("begin...");
            sleep(1.5);
            latch.countDown();
            log.debug("end...{}", latch.getCount());
        }).start();
        log.debug("waiting...");
        latch.await();
        log.debug("wait end...");
    }

输出

18:44:00.778 c.TestCountDownLatch [main] - waiting... 
18:44:00.778 c.TestCountDownLatch [Thread-2] - begin... 
18:44:00.778 c.TestCountDownLatch [Thread-0] - begin... 
18:44:00.778 c.TestCountDownLatch [Thread-1] - begin... 
18:44:01.782 c.TestCountDownLatch [Thread-0] - end...2 
18:44:02.283 c.TestCountDownLatch [Thread-2] - end...1 
18:44:02.782 c.TestCountDownLatch [Thread-1] - end...0 
18:44:02.782 c.TestCountDownLatch [main] - wait end... 

源码

public class CountDownLatch {
    
    private static final class Sync extends AbstractQueuedSynchronizer {
        private static final long serialVersionUID = 4982264981922014374L;

        Sync(int count) {
            setState(count);
        }

        int getCount() {
            return getState();
        }

        // 这里面去判断获得锁的条件 等不等于0,等于0就相当于获得了这个锁
        protected int tryAcquireShared(int acquires) {
            return (getState() == 0) ? 1 : -1;
        }

        // 如果其他线程调用 realease
        protected boolean tryReleaseShared(int releases) {
            // Decrement count; signal when transition to zero
            for (;;) {
                int c = getState();
                if (c == 0)
                    return false;
                int nextc = c-1;
                if (compareAndSetState(c, nextc))
                    // 什么时候 成为0了,就唤醒被阻塞的线程
                    return nextc == 0;
            }
        }
    }

    ...
}

可以配合线程池使用,改进如下

在这里说明一下,其实join也可以达到这样的效果,但是join是属于比较底层的api,而countDownlatch属于比较高级的api。

public static void main(String[] args) throws InterruptedException {
        CountDownLatch latch = new CountDownLatch(3);
        ExecutorService service = Executors.newFixedThreadPool(4);
        service.submit(() -> {
            log.debug("begin...");
            sleep(1);
            latch.countDown();
            log.debug("end...{}", latch.getCount());
        });
        service.submit(() -> {
            log.debug("begin...");
            sleep(1.5);
            latch.countDown();
            log.debug("end...{}", latch.getCount());
        });
        service.submit(() -> {
            log.debug("begin...");
            sleep(2);
            latch.countDown();
            log.debug("end...{}", latch.getCount());
        });
        service.submit(()->{
            try {
                log.debug("waiting...");
                latch.await();
                log.debug("wait end...");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
    }

输出

18:52:25.831 c.TestCountDownLatch [pool-1-thread-3] - begin... 
18:52:25.831 c.TestCountDownLatch [pool-1-thread-1] - begin... 
18:52:25.831 c.TestCountDownLatch [pool-1-thread-2] - begin... 
18:52:25.831 c.TestCountDownLatch [pool-1-thread-4] - waiting... 
18:52:26.835 c.TestCountDownLatch [pool-1-thread-1] - end...2 
18:52:27.335 c.TestCountDownLatch [pool-1-thread-2] - end...1 
18:52:27.835 c.TestCountDownLatch [pool-1-thread-3] - end...0 
18:52:27.835 c.TestCountDownLatch [pool-1-thread-4] - wait end... 

应用之同步等待多线程准备完毕

以经典的王者荣耀等待为例子,准备阶段,必须要十个人都准备好了,游戏才能开始,那么就这样来模拟

AtomicInteger num = new AtomicInteger(0);
    ExecutorService service = Executors.newFixedThreadPool(10, (r) -> {
        return new Thread(r, "t" + num.getAndIncrement());
    });
    CountDownLatch latch = new CountDownLatch(10);
    String[] all = new String[10];
    Random r = new Random();
	for (int j = 0; j < 10; j++) {
        int x = j;
        service.submit(() -> {
        	for (int i = 0; i <= 100; i++) {
        	try {
                // 因为加的是随机输出,所以会出现差异值
        		Thread.sleep(r.nextInt(100));
        	} catch (InterruptedException e) {
        	}
        	all[x] = Thread.currentThread().getName() + "(" + (i + "%") + ")";
        	System.out.print("\r" + Arrays.toString(all));
        }
        latch.countDown();
    });
}
latch.await();
System.out.println("\n游戏开始...");
service.shutdown();

中间输出

[t0(52%), t1(47%), t2(51%), t3(40%), t4(49%), t5(44%), t6(49%), t7(52%), t8(46%), t9(46%)] 

最后输出

[t0(100%), t1(100%), t2(100%), t3(100%), t4(100%), t5(100%), t6(100%), t7(100%), t8(100%), t9(100%)] 
游戏开始... 

应用之同步等待多个远程调用结束

@RestController
public class TestCountDownlatchController {
    @GetMapping("/order/{id}")
    public Map<String, Object> order(@PathVariable int id) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("id", id);
        map.put("total", "2300.00");
        sleep(2000);
        return map;
    }
    @GetMapping("/product/{id}")
    public Map<String, Object> product(@PathVariable int id) {

        HashMap<String, Object> map = new HashMap<>();
        if (id == 1) {
            map.put("name", "小爱音箱");
            map.put("price", 300);
        } else if (id == 2) {
            map.put("name", "小米手机");
            map.put("price", 2000);
        }
        map.put("id", id);
        sleep(1000);
        return map;
    }
    @GetMapping("/logistics/{id}")
    public Map<String, Object> logistics(@PathVariable int id) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("id", id);
        map.put("name", "中通快递");
        sleep(2500);
        return map;
    }
    private void sleep(int millis) {
        try {
            Thread.sleep(millis);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

rest 远程调用 CountDownLatch

 RestTemplate restTemplate = new RestTemplate();
        log.debug("begin");
        ExecutorService service = Executors.newCachedThreadPool();
        CountDownLatch latch = new CountDownLatch(4);
        service.submit(() -> {
            Map<String, Object> r =
                restTemplate.getForObject("http://localhost:8080/order/{1}", Map.class, 1);
            latch.CountDown();
            log.debug(r);
        });
        service.submit(() -> {
            Map<String, Object> r =
                restTemplate.getForObject("http://localhost:8080/product/{1}", Map.class, 1);
            latch.CountDown();
            log.debug(r);
        });
        = service.submit(() -> {
            Map<String, Object> r =
                restTemplate.getForObject("http://localhost:8080/product/{1}", Map.class, 2);
            latch.CountDown();
            log.debug(r);
        });
        = service.submit(() -> {
            Map<String, Object> r =
                restTemplate.getForObject("http://localhost:8080/logistics/{1}", Map.class, 1);
            latch.CountDown();
            log.debug(r);
        });
        latch.await();
        log.debug("执行完毕");
        service.shutdown();

rest 远程调用 future 带返回值

RestTemplate restTemplate = new RestTemplate();
        log.debug("begin");
        ExecutorService service = Executors.newCachedThreadPool();
        CountDownLatch latch = new CountDownLatch(4);
        Future<Map<String,Object>> f1 = service.submit(() -> {
            Map<String, Object> r =
                restTemplate.getForObject("http://localhost:8080/order/{1}", Map.class, 1);
            return r;
        });
        Future<Map<String, Object>> f2 = service.submit(() -> {
            Map<String, Object> r =
                restTemplate.getForObject("http://localhost:8080/product/{1}", Map.class, 1);
            return r;
        });
        Future<Map<String, Object>> f3 = service.submit(() -> {
            Map<String, Object> r =
                restTemplate.getForObject("http://localhost:8080/product/{1}", Map.class, 2);
            return r;
        });
        Future<Map<String, Object>> f4 = service.submit(() -> {
            Map<String, Object> r =
                restTemplate.getForObject("http://localhost:8080/logistics/{1}", Map.class, 1);
        return r;
        });
        System.out.println(f1.get());
        System.out.println(f2.get());
        System.out.println(f3.get());
        System.out.println(f4.get());
        log.debug("执行完毕");
        service.shutdown();

执行结果会比同步快非常多

但是需要注意的是,没有返回结果的时候,用countdownlatch比较合适,如果有返回值结果的话,还有用future

CyclicBarrier

public static void main(String[] args) throws InterruptedException {
        CountDownLatch latch = new CountDownLatch(3);
    	ExecutorService service = Executors.newFixedThreadPool(5);
    	service.submit(() -> {
            log.debug("task1 start...");
            sleep(1);
            latch.countDown();
        });
        service.submit(() -> {
            log.debug("task2 start...");
            sleep(2);
            latch.countDown();
        });
    	try{
            latch.await();
        }catch(InterruptedException e){
            e.printStackTrace();
        }
        log.debug("task1 task2 finish ...");
    	service.shutdown();
    }

但是目前的需求是 task1 task2 被反复的运行三遍

最简单的办法就是将上述的代码放置到for循环中去

public static void main(String[] args) throws InterruptedException {
        
    	ExecutorService service = Executors.newFixedThreadPool(5);
    	for(int i = 0 ; i < 3 ; i++){
            CountDownLatch latch = new CountDownLatch(3);
            service.submit(() -> {
            	log.debug("task1 start...");
            	sleep(1);
            	latch.countDown();
        	});
        	service.submit(() -> {
            	log.debug("task2 start...");
            	sleep(2);
            	latch.countDown();
        	});
    		try{
            	latch.await();
        	}catch(InterruptedException e){
            	e.printStackTrace();
        	}
        }
        log.debug("task1 task2 finish ...");
    	service.shutdown();
    }

这样依然能够实现功能,但是,其实CountDownLatch也被创建了三次。

那能不能重用呢?这个CountDownLatch只能在构造方法的时候,给一个初始值,以后就不能改了。

[ˈsaɪklɪk ˈbæriɚ] 循环栅栏,用来进行线程协作,等待线程满足某个计数。构造时设置『计数个数』,每个线程执
行到某个需要“同步”的时刻调用 await() 方法进行等待,当等待的线程数满足『计数个数』时,继续执行

CyclicBarrier cb = new CyclicBarrier(2); // 个数为2时才会继续执行
ExecutorService service = Executors.newFixedThreadPool(2);
service.submit(()->{
 	System.out.println("线程1开始.."+new Date());
 	try {
 		cb.await(); // 当个数不足时,等待
 	} catch (InterruptedException | BrokenBarrierException e) {
 		e.printStackTrace();
 	}
 	System.out.println("线程1继续向下运行..."+new Date());
});

service.submit(()->{
 	System.out.println("线程2开始.."+new Date());
 	try { Thread.sleep(2000); } catch (InterruptedException e) { }
 		try {
 			cb.await(); // 2 秒后,线程个数够2,继续运行
 		} catch (InterruptedException | BrokenBarrierException e) {
 			e.printStackTrace();
 	}
 	System.out.println("线程2继续向下运行..."+new Date());
});

当await等于0的时候,此时就能够继续往下运行了。

CyclicBarrier cb = new CyclicBarrier(2()->{
    // 等到两个都执行完成后,就会执行这里面的方法
	log.debug("finish");
}); // 个数为2时才会继续执行

ExecutorService service = Executors.newFixedThreadPool(2);

service.submit(()->{
 	System.out.println("线程1开始.."+new Date());
 	try {
 		cb.await(); // 当个数不足时,等待
 	} catch (InterruptedException | BrokenBarrierException e) {
 		e.printStackTrace();
 	}
 	System.out.println("线程1继续向下运行..."+new Date());
});

service.submit(()->{
 	System.out.println("线程2开始.."+new Date());
 	try { Thread.sleep(2000); } catch (InterruptedException e) { }
 		try {
 			cb.await(); // 2 秒后,线程个数够2,继续运行
 		} catch (InterruptedException | BrokenBarrierException e) {
 			e.printStackTrace();
 	}
 	System.out.println("线程2继续向下运行..."+new Date());
});

再次调用await就会恢复成2,在一个for循环中调用

CyclicBarrier cb = new CyclicBarrier(2()->{
    // 等到两个都执行完成后,就会执行这里面的方法
	log.debug("finish");
}); // 个数为2时才会继续执行

ExecutorService service = Executors.newFixedThreadPool(2);
for(int i = 0; i < 3 ; i++){
	service.submit(()->{
 		System.out.println("线程1开始.."+new Date());
 		try {
 			cb.await(); // 当个数不足时,等待
 		} catch (InterruptedException | BrokenBarrierException e) {
 			e.printStackTrace();
 		}
 		System.out.println("线程1继续向下运行..."+new Date());
	});

	service.submit(()->{
 		System.out.println("线程2开始.."+new Date());
 		try { Thread.sleep(2000); } catch (InterruptedException e) { }
 			try {
 				cb.await(); // 2 秒后,线程个数够2,继续运行
 			} catch (InterruptedException | BrokenBarrierException e) {
 				e.printStackTrace();
 		}
 		System.out.println("线程2继续向下运行..."+new Date());
	});
}

这样就达到了CountDownLatch的效果啦

在这里面需要注意一下,就是 希望线程池的线程数 和 屏障数是一致的

如果不一致,比如说线程池是 三个核心线程,那么当继续走for循环的时候,由于cyclicbarrier的特性,此时就是两个task1 执行完成了,而task2比较慢。这样有可能会影响最终的效果。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1195483.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

AD9371 Crossbar 和 I、Q数据 映射JESD204B传输层

AD9371 系列快速入口 AD9371ZCU102 移植到 ZCU106 &#xff1a; AD9371 官方例程构建及单音信号收发 ad9371_tx_jesd -->util_ad9371_xcvr接口映射&#xff1a; AD9371 官方例程之 tx_jesd 与 xcvr接口映射 AD9371 官方例程 时钟间的关系与生成 &#xff1a; AD9371 官方…

vim相关命令讲解!

本文旨在讲解vim 以及其相关的操作&#xff01; 希望读完本文&#xff0c;读者会有一定的收获&#xff01;好的&#xff0c;干货马上就来&#xff01; 初识vim 在讲解vim之前&#xff0c;我们首先要了解vim是什么&#xff0c;有什么作用&#xff1f;只有了解了vim才能更好的理…

ModStartBlog v8.5.0 评论开关布局调整,系统后台全面优化

ModStart 是一个基于 Laravel 模块化极速开发框架。模块市场拥有丰富的功能应用&#xff0c;支持后台一键快速安装&#xff0c;让开发者能快的实现业务功能开发。 系统完全开源&#xff0c;基于 Apache 2.0 开源协议。 功能特性 丰富的模块市场&#xff0c;后台一键快速安装 …

怎么用电脑开发安卓app?能外包吗?

随着智能手机的普及&#xff0c;安卓应用程序的开发需求也越来越高&#xff0c;许多人都想开发自己的安卓应用程序&#xff0c;但苦于缺乏相关知识和技能&#xff0c;本文将介绍如何使用电脑开发安卓应用程序&#xff0c;以及是否可以将开发工作外包给专业的开发团队。 一、了…

7天入门python系列之第三天Python的函数和模块

第3天主要是学习Python的函数和模块 编者打算开一个python 初学主题的系列文章&#xff0c;用于指导想要学习python的同学。关于文章有任何疑问都可以私信作者。对于初学者想在7天内入门Python&#xff0c;这是一个紧凑的学习计划。但并不是不可完成的。第三天开始python函数和…

python3GUI--PyQt5打包心得(二)nuitka、inno Setup(详细图文演示、附所有软件)

文章目录 一&#xff0e;前言二&#xff0e;准备1.nuitka1.1介绍1.3项目地址1.3安装 2.mingw641.1介绍1.2下载安装 3.Inno Setup1.1介绍1.2安装 三&#xff0e;nuitka打包1.打包2.装mingw643.装ccahe4.打包完成 四&#xff0e;测试效果五&#xff0e;inno Setup制作安装软件1.配…

呼叫中心系统如果对接大模型

电话机器人对接大模型的例子 介绍 自chatgpt3.5发布以来&#xff0c;各种大模型飞速发展&#xff0c;各行各业都有接入大模型的需求&#xff0c;呼叫中心行业非常适合通过接入大模型用AI来回答用户的各种咨询&#xff0c;降低人力资源&#xff0c;使用顶顶通呼叫中心中间件&a…

日志收集的方式和优点

日志是组织 IT 环境中发生的所有事情的记录。它们通常是一系列带有时间戳的消息&#xff0c;可为您提供有关网络中所有活动的第一手信息。 网络中的每个设备和应用程序都会生成日志数据以及用于监控网络流量的 NetFlow 数据&#xff0c;日志是安全信息和事件管理&#xff08;S…

宠物医院信息展示预约小程序的效果如何

养宠家庭越来越多&#xff0c;随之带来的就是宠物健康问题&#xff0c;生活条件稍微好点的家庭&#xff0c;只要宠物生病或洗护、寄养、美容等就会前往宠物医院&#xff0c;而近些年来&#xff0c;市场中的宠物医院也在连年增加&#xff0c;可以预见市场需求度较高。 而对宠物…

Linux shell编程学习笔记23:[] [[]]的用法小结

上回梳理 了Linux Shell编程中 () 、$()和 (())的用法&#xff0c;现在接着梳理 [] 和[[]]的用法。 1 单中括号&#xff08;方括号&#xff09;[] 1.1 检测某个条件是否成立 [和test等同&#xff0c;是 Shell 内置命令&#xff0c;用来检测某个条件是否成立。条件成立时退出状…

vue实战——登出【详解】

登出逻辑 弹窗询问用户是否确定登出清除登录状态 登录状态通常存储在 vuex 和 sessionStorage 中&#xff0c;更彻底的登出还可以把所有本地存储数据都清除掉&#xff0c;如 Cookie 和 localStorage 。跳转到登录页面 代码实现 <div class"loginBox" v-if"is…

操作系统:输入输出管理(一)系统概述与设备独立性软件

一战成硕 5.1 I/O系统概述5.1.1 I/O设备5.1.2 I/O控制方式5.1.3 I/O软件层次结构5.1.4 应用程序的I/O接口 5.2 设备独立性软件5.2.1 与设备无关的软件5.2.2 高速缓存与缓冲区5.2.3 设备分配与回收5.2.4 spooling技术&#xff08;假脱机技术&#xff09; 5.1 I/O系统概述 5.1.1…

合并两个链表 --- 递归回溯算法练习二

目录 1. 分析题意 2. 分析算法原理 2.1. 递归思路&#xff1a; 1. 挖掘子问题&#xff1a; 3. 编写代码 3.1. step one 3.2. step two 3.3. step three 3.1. 递归写法 4. 补充 --- 迭代写法 5. 总结 1. 分析题意 力扣上原题链接如下&#xff1a; 21. 合并两个有序链表…

ubuntu16.04 交叉编译 mbedtls

在为客户交叉编译项目时需要依赖 mbedtls&#xff0c; 客户的机器是 arm64 的 ubuntu 16.04&#xff0c; 交叉编译过程中遇到几个问题。 首先&#xff0c; mbedtls 需要依赖 python, 在 cmake 的过程中&#xff0c; 如果不是使用系统默认的 cmake 可能会导致&#xff0c;mbedt…

6.4翻转二叉树(LC226—送分题,前序遍历)

算法&#xff1a; 第一想法是用昨天的层序遍历&#xff0c;把每一层level用切片反转。但是这样时间复杂度很高。 其实只要在遍历的过程中去翻转每一个节点的左右孩子就可以达到整体翻转的效果。 这道题目使用前序遍历和后序遍历都可以&#xff0c;唯独中序遍历不方便&#x…

赛氪中西部外语翻译大赛入榜2023国内翻译赛事发展评估报告

中西部外语翻译大赛入选中国外文局CATTI项目管理中心和中国外文界平台联合发布《2023国内翻译赛事发展评估报告》 近日&#xff0c;中国外文局CATTI项目管理中心和中国外文界平台联合发布了《2023国内翻译赛事发展评估报告》&#xff0c;报告对国内主流外语翻译赛事进行了问卷调…

Centos8安装出错问题

科普介绍&#xff1a; CentOS 8 是一个基于 Linux 的操作系统&#xff0c;是 Red Hat Enterprise Linux &#xff08;RHEL&#xff09;的免费和开源版本。它提供了稳定、安全和可靠的基础设施&#xff0c;适用于服务器和桌面环境。CentOS 8 是 CentOS 系列中最新的版本&#x…

Nginx(五)

负载均衡 官网文档 Using nginx as HTTP load balancer nginx中实现反向代理的方式 HTTP&#xff1a;通过nginx配置反向代理到后端服务器&#xff0c;nginx将接收到的HTTP请求转发给后端服务器。使用 proxy_pass 命令 HTTPS&#xff1a;通过nginx配置反向代理到后端服务器&…

Amazon Aurora MySQL 与 Amazon Redshift 的 Zero ETL 集成已全面可用,一起轻松上手!

“数据是应用、流程和商业决策的核心。” 亚马逊云科技数据库、 数据分析和机器学习全球副总裁 Swami Sivasubramanian 如今&#xff0c;客户常用的数据传输模式是建立从 Amazon Aurora 到 Amazon Redshift 的数据管道。这些解决方案能够帮助客户获得新的见解&#xff0c;进而…

【狂神说Java】linux详解

✅作者简介&#xff1a;CSDN内容合伙人、信息安全专业在校大学生&#x1f3c6; &#x1f525;系列专栏 &#xff1a;狂神说Java &#x1f4c3;新人博主 &#xff1a;欢迎点赞收藏关注&#xff0c;会回访&#xff01; &#x1f4ac;舞台再大&#xff0c;你不上台&#xff0c;永远…