Java Problems with Solutions
Find Duration in Hours and Minutes Between Two Dates in Java
Sort a Java List Alphabetically First and then by Numbers
Encryption and Decryption of Strings in Java
Generate a Random String of Specific Length in Java
Split a String by Comma and Space in Java
Remove Duplicates from a Java List
Converting HashSet to List in Java
Calculate Days Between Two Dates in Java
Round a Number to N decimal Places in Java
Java Run Multiple Tasks Simultaneously
Filling Values in Editable PDF Forms in Java
Create ICS Calendar File in Java/Spring Boot
Convert LocalDateTime to Milliseconds in Java
Convert String DateTime To LocalDateTime in Java
Convert LocalDateTime to String in Java
Convert JSON Array to List of Long Type in Java
Convert a List to an Array in Java
Convert an Array to a List in Java
Convert POJO Class Object to JSON String in Java
Convert JSON String to POJO Class Object in Java
How to Add Data to a List in Java
How to avoid Deadlock in Java with Example
Counting Unique Numbers in a List of Integers
Extract Created and Modified Datetime of Files in Java
Remove Matching Items from List Ignoring Case
Java Solved Programs and Problems with Solutions
Java LocalDateTime Formatting
Merge Sets and Convert to List in Java
Working with CSV files in Java: Read and Write Data with Ease
Resolving Java Type Mismatch Error: Cannot Convert from List<int[]> to List<Integer>
Validating Email with Regex in Java
Java Run Multiple Tasks Simultaneously
- Last updated Apr 26, 2024
Sometimes we may need to write a program that can process multiple tasks concurrently. Java supports multithreading which means that we can run multiple threads to do different task at the same time within a single program.
In this example, we will use Java built-in ExecutorService from the java.util.concurrent package to execute multiple tasks asynchronously.
Here's an example code that demonstrates running of 1000 tasks concurrently at one time:
import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; public class Example { public static void main(String[] args) { // 50000 tasks ListThe example code starts by importing necessary classes from the Java API. A class called "Example" is defined, which contains a main method as the entry point of the program. In the main method, a list of tasks is created using the Arrays.asList() method. Each task is represented by a number.The start time of the tasks is recorded using System.currentTimeMillis(). The initiateRequest method is called, passing the list of tasks as an argument. If the list is empty, the method returns early. The numberOfThreads variable is set to 1000, indicating the maximum number of tasks that can run concurrently. An ExecutorService is created using Executors.newFixedThreadPool(numberOfThreads), which creates a pool of threads to execute the tasks. A list called "futureList" is created to store the results of the tasks. The forEach method is used to iterate over each task in the list and submit it to the ExecutorService using the submit() method. The resulting Future object is added to the futureList. A loop is used to iterate over the futureList. For each Future object, the get() method is called to retrieve the result of the corresponding task. This is a blocking operation, meaning it waits until the task completes and returns a result. The retrieved result is printed to the console. After all tasks are completed, the ExecutorService is shut down to release the allocated resources. The completion time of the tasks is recorded using System.currentTimeMillis(), and the elapsed time is calculated in seconds. Finally, the total time taken and the number of tasks completed are printed to the console.tasks = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 50000); // task start time long startTime = System.currentTimeMillis(); initiateRequest(tasks); // task end time long endTime = System.currentTimeMillis(); double timeTaken = (endTime - startTime) / 1000; System.out.println("Time taken to run " + tasks.size() + " urls = " + timeTaken + " seconds"); } public static void initiateRequest(List tasks) { if (tasks.isEmpty()) { return; } int numberOfThreads = 1000; ExecutorService fixedPoolExecutor = Executors.newFixedThreadPool(numberOfThreads); List > futureList = new ArrayList<>(); tasks.forEach(url -> { Future future = fixedPoolExecutor.submit(() -> myTask(url)); futureList.add(future); }); for (Future future : futureList) { try { System.out.println("Response received = " + future.get()); } catch (InterruptedException | ExecutionException e) { e.printStackTrace(); } } System.out.println("Complete"); } private static String myTask(int taskId) { System.out.println("Started task ID : " + taskId); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Completed task ID : " + taskId); return "Task ID : " + taskId; } }
In summary, the code demonstrates how to run multiple tasks concurrently using an ExecutorService and retrieve the results of the tasks as they complete. The tasks are executed by a pool of threads, allowing for efficient parallel processing.