首页 > 其他分享 >jdk20 Structured Concurrency 结构化并发官网示例

jdk20 Structured Concurrency 结构化并发官网示例

时间:2023-04-26 17:44:45浏览次数:36  
标签:fork jdk20 tasks 示例 Structured var scope forks

此特性还在孵化,后续版本可能有变动

 //全部执行直到有失败的任务
    String handle() throws ExecutionException, InterruptedException {
        try (var scope = new StructuredTaskScope.ShutdownOnFailure()) {
            Future<String>  user  = scope.fork(() -> "");
            Future<Integer> order = scope.fork(() -> 1);

            scope.join();           // Join both forks
            scope.throwIfFailed();  // ... and propagate errors

            // Here, both forks have succeeded, so compose their results
          return   user.resultNow()+order;

        }
    }
    //有一个任务成功就会关闭资源返回结果
    <T> T race(List<Callable<T>> tasks, Instant deadline) throws ExecutionException, InterruptedException, TimeoutException {
        try (var scope = new StructuredTaskScope.ShutdownOnSuccess<T>()) {
            for (var task : tasks) {
                scope.fork(task);
            }
            scope.joinUntil(deadline);
            return scope.result();  // Throws if none of the forks completed successfully
        }
    }

标签:fork,jdk20,tasks,示例,Structured,var,scope,forks
From: https://www.cnblogs.com/funkboy/p/17356830.html

相关文章

  • Python pandas 保存Excel自动调整列宽的方法及示例代码
    方法1.  用pd.ExcelWriter和  worksheet.set_column 需要安装xlsxwriter方法2:使用StyleFrame自动调整fromstyleframeimportStyleFrameimportpandasaspdcolumns=['aaaaaaaaaaa','bbbbbbbbb','ccccccccccc',]df=pd.DataFrame(data={......
  • golang 使用 net包实现 tcp server 示例
    之前用到golang进行网络编程时,主要就是使用net/http和web框架gin,这些网络库的底层其实也还是用的标准库自带的net包,很多是对路由或者其他做封装,而且golang本身的长处之一也是网络IO的处理,这也得益于其底层的IO模型,今天我们分享的是基于TCPserver/client的简单实现,后......
  • 40 个简单又有效的 Linux Shell 脚本示例 转载
    1.HelloWorld程序员经常通过学习helloworld程序来学习新语言。这是一个简单的程序,将字符串“HelloWorld”打印到标准输出中。然后,使用vim或nano等编辑器创建hello-world.sh文件,并将以下行复制到其中。#!/bin/bashecho "Hello World"保存并退出文件。接下来,您需要......
  • okhttp3的基本使用(post发送json示例)
    依赖<!--https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp--><dependency><groupId>com.squareup.okhttp3</groupId><artifactId>okhttp</artifactId><ve......
  • hello flume (Ubuntu 下 flume1.5单机版安装以及简单入门示例)
    1,下载最新的flume安装包:wgethttp://www.apache.org/dist/flume/stable/apache-flume-1.5.2-bin.tar.gz2,在安装目录解压:tar-zxvfapache-flume-1.5.2-bin.tar.gz3,设置环境变量exportJAVA_HOME=/usr;exportFLUME_HOME=/home/joeyon/apache-flume-1.5.2-bin;exportPATH=$PAT......
  • jenkins 用法示例
    示例1:根据参数进行构建  根据参数的值进行不同的操作。#!/bin/bashecho"获取值"echo$Deploycase$Deployin"deploy1")echo123>>/opt/script/aa.log;;"rollback1")echo456>>/opt/script/aa.log;;esac ......
  • Linux基础知识(17)- Kerberos (二) | krb5 API 的 C 程序示例
    在“Linux基础知识(16)-Kerberos(一)|Kerberos安装配置”里我们演示了Kerberos安装配置和Kadmin等命令行工具的用法,本文将演示krb5API的使用方法。Krb5API:http://web.mit.edu/kerberos/krb5-current/doc/appldev/refs/api/index.html 1.系统环境   操作......
  • Watch监听-示例
    packagecom.hw.curator;importorg.apache.curator.RetryPolicy;importorg.apache.curator.framework.CuratorFramework;importorg.apache.curator.framework.CuratorFrameworkFactory;importorg.apache.curator.framework.api.BackgroundCallback;importorg.apach......
  • python三角网格划分示例
    python三角网格划分示例 importnumpyasnpimportturtle#输入三角形的边长length=float(input("Enterthelengthofthetriangle:"))#计算最短边、最长边和三角形个数short_side=lengthmax_side=length+lengthn=int(max_side/s......
  • 正的浮点数相加的示例程序 Verilog
    modulefloat_adder( input        clk, input       rst_n, input        en, input   [31:0]  aIn, input   [31:0]  bIn, outputreg     busy, outputreg   out......