java并发编程实战-CyclicBarrier和FutureTask使用

2024-04-08 03:48

本文主要是介绍java并发编程实战-CyclicBarrier和FutureTask使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

import java.util.concurrent.Callable;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.FutureTask;public class FutureTest {public static void main(String[] args) {new FutureTest().test();}public void test() {// 设置7个线程栅栏,为什么6个线程要设置7个栅栏,因为需要阻塞本线程等待6个线程的执行结果final CyclicBarrier cb = new CyclicBarrier(7);// 创建6个服务final Service[] assembles = new Service[6];final FutureTask[] futureTasks = new FutureTask[6];final Domain domain = new Domain();assembles[0] = new Service0();assembles[1] = new Service1();assembles[2] = new Service2();assembles[3] = new Service3();assembles[4] = new Service4();assembles[5] = new Service5();for(int i = 0; i < 6; i++) {final int fi = i;futureTasks[i] = new FutureTask<Integer>(new Callable<Integer>() {  @Override  public Integer call() throws Exception { try {assembles[fi].assemble(domain);System.out.println(fi + "assemble");} finally {cb.await();}return fi;}  });new Thread(futureTasks[i]).start(); }try {cb.await();} catch (Exception e) {System.err.println(e);}for(int i = 0; i < 6; i++) {try {System.out.println(futureTasks[i].get());} catch (Exception e) {// 获取Service5抛出的异常System.err.println(e);} }System.out.println(domain);}interface Service {void assemble(Domain domian);}class Domain{public String t0;public String t1;public String t2;public String t3;public String t4;@Overridepublic String toString() {return t0 + "-" +t1 + "-" +t2 + "-" +t3 + "-" + t4;}}class Service0 implements Service {@Overridepublic void assemble(Domain domian) {try {Thread.sleep(2000);} catch (InterruptedException e) {System.err.println(e);}domian.t0 = "0";}}class Service1 implements Service {@Overridepublic void assemble(Domain domian) {try {Thread.sleep(4000);} catch (InterruptedException e) {System.err.println(e);}domian.t1 = "1";}}class Service2 implements Service {@Overridepublic void assemble(Domain domian) {try {Thread.sleep(2000);} catch (InterruptedException e) {System.err.println(e);}domian.t2 = "2";}}class Service3 implements Service {@Overridepublic void assemble(Domain domian) {try {Thread.sleep(3000);} catch (InterruptedException e) {System.err.println(e);}domian.t3 = "3";}}class Service4 implements Service {@Overridepublic void assemble(Domain domian) {try {Thread.sleep(1000);} catch (InterruptedException e) {System.err.println(e);}domian.t4 = "4";}}class Service5 implements Service {@Overridepublic void assemble(Domain domian) {try {Thread.sleep(5000);} catch (InterruptedException e) {System.err.println(e);}throw new RuntimeException();}}
}

这篇关于java并发编程实战-CyclicBarrier和FutureTask使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


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

相关文章

Java日期类详解(最新推荐)

《Java日期类详解(最新推荐)》早期版本主要使用java.util.Date、java.util.Calendar等类,Java8及以后引入了新的日期和时间API(JSR310),包含在ja... 目录旧的日期时间API新的日期时间 API(Java 8+)获取时间戳时间计算与其他日期时间类型的转换Dur

java对接海康摄像头的完整步骤记录

《java对接海康摄像头的完整步骤记录》在Java中调用海康威视摄像头通常需要使用海康威视提供的SDK,下面这篇文章主要给大家介绍了关于java对接海康摄像头的完整步骤,文中通过代码介绍的非常详细,需... 目录一、开发环境准备二、实现Java调用设备接口(一)加载动态链接库(二)结构体、接口重定义1.类型

SpringBoot读取ZooKeeper(ZK)属性的方法实现

《SpringBoot读取ZooKeeper(ZK)属性的方法实现》本文主要介绍了SpringBoot读取ZooKeeper(ZK)属性的方法实现,强调使用@ConfigurationProperti... 目录1. 在配置文件中定义 ZK 属性application.propertiesapplicati

Java Multimap实现类与操作的具体示例

《JavaMultimap实现类与操作的具体示例》Multimap出现在Google的Guava库中,它为Java提供了更加灵活的集合操作,:本文主要介绍JavaMultimap实现类与操作的... 目录一、Multimap 概述Multimap 主要特点:二、Multimap 实现类1. ListMult

Java中常见队列举例详解(非线程安全)

《Java中常见队列举例详解(非线程安全)》队列用于模拟队列这种数据结构,队列通常是指先进先出的容器,:本文主要介绍Java中常见队列(非线程安全)的相关资料,文中通过代码介绍的非常详细,需要的朋... 目录一.队列定义 二.常见接口 三.常见实现类3.1 ArrayDeque3.1.1 实现原理3.1.2

CnPlugin是PL/SQL Developer工具插件使用教程

《CnPlugin是PL/SQLDeveloper工具插件使用教程》:本文主要介绍CnPlugin是PL/SQLDeveloper工具插件使用教程,具有很好的参考价值,希望对大家有所帮助,如有错... 目录PL/SQL Developer工具插件使用安装拷贝文件配置总结PL/SQL Developer工具插

SpringBoot整合Apache Flink的详细指南

《SpringBoot整合ApacheFlink的详细指南》这篇文章主要为大家详细介绍了SpringBoot整合ApacheFlink的详细过程,涵盖环境准备,依赖配置,代码实现及运行步骤,感兴趣的... 目录1. 背景与目标2. 环境准备2.1 开发工具2.2 技术版本3. 创建 Spring Boot

springboot加载不到nacos配置中心的配置问题处理

《springboot加载不到nacos配置中心的配置问题处理》:本文主要介绍springboot加载不到nacos配置中心的配置问题处理,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑... 目录springboot加载不到nacos配置中心的配置两种可能Spring Boot 版本Nacos

SpringBoot3中使用虚拟线程的完整步骤

《SpringBoot3中使用虚拟线程的完整步骤》在SpringBoot3中使用Java21+的虚拟线程(VirtualThreads)可以显著提升I/O密集型应用的并发能力,这篇文章为大家介绍了详细... 目录1. 环境准备2. 配置虚拟线程方式一:全局启用虚拟线程(Tomcat/Jetty)方式二:异步

Java反射实现多属性去重与分组功能

《Java反射实现多属性去重与分组功能》在Java开发中,​​List是一种非常常用的数据结构,通常我们会遇到这样的问题:如何处理​​List​​​中的相同字段?无论是去重还是分组,合理的操作可以提高... 目录一、开发环境与基础组件准备1.环境配置:2. 代码结构说明:二、基础反射工具:BeanUtils