🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
The CompletableFuture class in Java provides the runAsync() method to run a task asynchronously without returning a result.
Introduction
The CompletableFuture.runAsync() method is used to run a task asynchronously and return a CompletableFuture that will be completed when the task is finished. This is useful in asynchronous programming when you need to perform non-blocking operations that do not return a result.
runAsync Method Syntax
The syntax for the runAsync method is as follows:
public static CompletableFuture<Void> runAsync(Runnable runnable)
- The method takes a single parameter
runnableof typeRunnable, which represents the task to run. - The method returns a
CompletableFuture<Void>that will be completed when the task is finished.
Examples
Example 1: Logging a Message
In a web application, you might want to log a message asynchronously to avoid blocking the main thread.
import java.util.concurrent.CompletableFuture;
public class LogMessageExample {
public static void main(String[] args) {
// Log a message asynchronously
CompletableFuture<Void> logFuture = CompletableFuture.runAsync(() -> {
// Simulate delay
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Logged message: Task completed successfully");
});
// Wait for the logging to complete
logFuture.join();
}
}
Output:
Logged message: Task completed successfully
Example 2: Updating a Status
In a web application, you might want to update a status asynchronously without waiting for the update to complete.
import java.util.concurrent.CompletableFuture;
public class UpdateStatusExample {
public static void main(String[] args) {
// Update status asynchronously
CompletableFuture<Void> statusFuture = CompletableFuture.runAsync(() -> {
// Simulate status update
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Status updated to: Completed");
});
// Continue with other tasks while status is being updated
System.out.println("Continuing with other tasks...");
// Wait for the status update to complete
statusFuture.join();
}
}
Output:
Continuing with other tasks...
Status updated to: Completed
Example 3: Task Management System
In a task management system, you might want to archive a completed task asynchronously to improve performance.
import java.util.concurrent.CompletableFuture;
public class TaskManagementSystem {
public static void main(String[] args) {
// Archive a task asynchronously
CompletableFuture<Void> archiveFuture = CompletableFuture.runAsync(() -> {
// Simulate task archiving
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Task archived successfully");
});
// Continue with other tasks while archiving is in progress
System.out.println("Processing other tasks...");
// Wait for the task archiving to complete
archiveFuture.join();
}
}
Output:
Processing other tasks...
Task archived successfully
Conclusion
The CompletableFuture.runAsync() method in Java is used for running tasks asynchronously that do not return a result. It is particularly useful in scenarios where non-blocking operations are required, such as logging messages, updating statuses, or archiving tasks in a task management system.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment