Structured Concurrency in Java Last Updated : 14 May, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report Structured concurrency organizes concurrent processes in a controlled way to ease concurrent execution. It prevents race situations, deadlocks, and orphaned jobs. Structured concurrency may be implemented in Java utilizing libraries, frameworks, or experimental initiatives like Project Loom. Structured concurrency concepts and techniques in Java: ExecutorService and CompletableFutureForkJoinPool and RecursiveTaskExecutorService and CompletableFuture ExecutorService in java.util.concurrent manages a pool of worker threads that may perform tasks simultaneously. CompletableFuture lets you link, manage, and combine tasks. ExecutorService and CompletableFuture provide systematic concurrency management of concurrent processes. Example: Java // Java Program to demonstrate structured concurrency in // Java using the ExecutorService and CompletableFuture // classes from the java.util.concurrent package. import java.util.concurrent.*; // Driver class public class StructuredConcurrencyExample { // function main public static void main(String[] args) throws InterruptedException, ExecutionException { ExecutorService executor = Executors.newFixedThreadPool(3); CompletableFuture<Void> task1 = CompletableFuture.runAsync(() -> { System.out.println("Task 1 started"); // Perform task 1 }, executor); CompletableFuture<Void> task2 = CompletableFuture.runAsync(() -> { System.out.println("Task 2 started"); // Perform task 2 }, executor); CompletableFuture<Void> combinedTasks = CompletableFuture.allOf(task1, task2); // Waits for both tasks to complete combinedTasks.get(); executor.shutdown(); } } OutputTask 1 started Task 2 started Note: The order of the task outputs may vary because the tasks are executed concurrently, and the order in which they complete may not be the same each time. ForkJoinPool and RecursiveTask ForkJoinPool, another concurrency framework in the java.util.concurrent package efficiently executes divide-and-conquer algorithms and steals work. ForkJoinPool with RecursiveTask or RecursiveAction creates organized concurrent tasks. Example: Java // Java program to demonstrate the use of the ForkJoinPool // to create organized concurrent tasks. import java.util.concurrent.*; public class StructuredConcurrencyExample { public static void main(String[] args) { ForkJoinPool forkJoinPool = new ForkJoinPool(); // Task 1 RecursiveAction task1 = new RecursiveAction() { @Override protected void compute() { // Perform task 1 System.out.println("Task 1 started"); } }; // Task 2 RecursiveAction task2 = new RecursiveAction() { @Override protected void compute() { // Perform task 2 System.out.println("Task 2 started"); } }; // Submit both tasks and then join them ForkJoinTask<Void> submittedTask1 = forkJoinPool.submit(task1); ForkJoinTask<Void> submittedTask2 = forkJoinPool.submit(task2); // Wait for both tasks to complete submittedTask1.join(); submittedTask2.join(); forkJoinPool.shutdown(); } } OutputTask 1 started Task 2 started Note: The order of the task outputs may vary because the tasks are executed concurrently, and the order in which they complete may not be the same each time. Comment More infoAdvertise with us Next Article Structured Concurrency in Java S sanketnagare Follow Improve Article Tags : Java Java Concurrency Practice Tags : Java Similar Reads Java Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s 10 min read Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it, 13 min read Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per 15+ min read Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co 11 min read Arrays in Java Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me 15+ min read Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac 15+ min read Inheritance in Java Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an 13 min read Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance 10 min read Java Exception Handling Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt 10 min read Java Programs - Java Programming Examples In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its 8 min read Like