欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java 多線程等待優(yōu)雅的實(shí)現(xiàn)方式之Phaser同步屏障

 更新時(shí)間:2021年11月10日 10:59:41   作者:劍客阿良_ALiang  
在JAVA 1.7引入了一個(gè)新的并發(fā)API:Phaser,一個(gè)可重用的同步barrier。在此前,JAVA已經(jīng)有CyclicBarrier、CountDownLatch這兩種同步barrier,但是Phaser更加靈活,而且側(cè)重于 重用

前言

是否會(huì)遇到這樣的場(chǎng)景,你向線程池提交了多個(gè)任務(wù),你希望這批任務(wù)全部完成后能夠反向通知你。

你可能會(huì)使用線程計(jì)數(shù)的方式,等到計(jì)數(shù)器累加到提交的線程數(shù)量,然后通知。emmm,不是不可以,只是不夠優(yōu)雅。本文提供優(yōu)雅的實(shí)現(xiàn)方式,Phaser同步屏障。

Maven依賴

也可以不依賴,本人習(xí)慣把代碼簡(jiǎn)單化,使用了hutool,所以依賴只有這個(gè)。

        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.7.15</version>
        </dependency>

代碼

廢話不多說,上代碼。

package com.huyi.csdn.tools;
 
import cn.hutool.core.thread.ThreadUtil;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Phaser;
import java.util.concurrent.TimeUnit;
 
/**
 * @Program: csdn @ClassName: PhaserUtil @Author: huyi @Date: 2021-11-06 21:03 @Description:
 * 多線程監(jiān)控回調(diào)工具 @Version: V1.0
 */
public class PhaserUtil {
  public static final ExecutorService executorService = Executors.newFixedThreadPool(50);
 
  public static class CustomPharser extends Phaser {
    private final Runnable runnable;
 
    public CustomPharser(Runnable runnable) {
      this.runnable = runnable;
    }
 
    @Override
    protected boolean onAdvance(int phase, int registeredParties) {
      this.runnable.run();
      return super.onAdvance(phase, registeredParties);
    }
  }
 
  /**
   * 提交任務(wù)以及完成后需要執(zhí)行的內(nèi)容
   *
   * @param tasks 任務(wù)
   * @param complete 完成任務(wù)
   */
  public static void submit(List<Runnable> tasks, Runnable complete) {
    Phaser phaser = new CustomPharser(complete);
    for (Runnable runnable : tasks) {
      executorService.submit(
          () -> {
            phaser.register();
            runnable.run();
            System.out.println(Thread.currentThread().getName() + "完成任務(wù)!");
            phaser.arriveAndAwaitAdvance();
          });
    }
  }
 
  /** 摧毀線程池 */
  public static void destroy() {
    System.out.println("摧毀線程池");
    executorService.shutdown();
  }
 
  public static void main(String[] args) {
    List<Runnable> tasks = new ArrayList<>();
    Random random = new Random();
    for (int i = 0; i < 10; i++) {
      tasks.add(
          () -> {
            ThreadUtil.sleep(random.nextInt(10), TimeUnit.SECONDS);
          });
    }
    submit(tasks, () -> System.out.println("所有任務(wù)已完成"));
    ThreadUtil.sleep(20, TimeUnit.SECONDS);
    destroy();
  }
}

代碼說明

1、提交任務(wù)執(zhí)行的方式是Runnable也好,Callable也好,或者Consumer、Function等等,不影響,你可以看著調(diào)整。

2、完成后的Runnable也和第一點(diǎn)同理。

驗(yàn)證一下

OK,沒什么問題。

總結(jié)

其實(shí)我一直想分享一些可以讓讀者工作中能用到的東西,想到牧神記里面的一句話,圣人之道,無非就是百姓日用。emmmm,又廢話了。

分享一下:

沒必要的事就不做,必要的事就盡快做。---冰果

如果本文對(duì)你有用,請(qǐng)不要吝嗇你的贊,謝謝。

以上就是Java 多線程等待優(yōu)雅的實(shí)現(xiàn)方式之Phaser同步屏障的詳細(xì)內(nèi)容,更多關(guān)于Java 多線程等待的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論