JS
JV

JavaScript to Java

10 lessons

Progress0%
1Variables & Types2Functions & Methods3Arrays & Collections4Classes & OOP5Exception Handling6Async vs Threads7Generics8String Methods9Interfaces and Abstract Classes10Build Tools and Ecosystem
All Mirror Courses
JS
JV
Async vs Threads
MirrorLesson 6 of 10
Lesson 6

Async vs Threads

Handling concurrent work

Introduction

In this lesson, you'll learn about async vs threads in Java. Coming from JavaScript, you already have a foundation for understanding this concept. We'll build on that knowledge while highlighting the key differences.

Mirror Card
JS
From JavaScript:

In JavaScript, you're familiar with handling concurrent work.

JV
In Java:

Java has its own approach to handling concurrent work, which we'll explore step by step.

The Java Way

Let's see how Java handles this concept. Here's a typical example:

JV
Java Example
import java.util.concurrent.*;

// CompletableFuture (Java 8+) — like JS Promise
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
    return fetchUser(1); // runs in thread pool
});

// Chain like .then()
future
  .thenApply(user -> "Hello, " + user)
  .thenAccept(System.out::println)
  .exceptionally(err -> { System.out.println("Error: " + err); return null; });

// Parallel — like Promise.all
CompletableFuture<String> userF = CompletableFuture.supplyAsync(() -> fetchUser(1));
CompletableFuture<String> postF = CompletableFuture.supplyAsync(() -> fetchPosts(1));
CompletableFuture.allOf(userF, postF).join();

Comparing to JavaScript

Here's how you might have written similar code in JavaScript:

JS
JavaScript (What you know)
// Async/await (single-threaded event loop)
async function fetchUser(id) {
  const res = await fetch("/api/users/" + id);
  return res.json();
}

// Parallel
const [user, posts] = await Promise.all([
  fetchUser(1),
  fetchPosts(1),
]);

// Error handling
try {
  const data = await fetchUser(1);
  console.log(data);
} catch (err) {
  console.error("Failed:", err);
}
Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

JV
In Java:

JS is single-threaded with an event loop; Java uses real threads

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

JV
In Java:

JS async/await → Java CompletableFuture (Java 8+)

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

JV
In Java:

JS Promise.all → CompletableFuture.allOf

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

JV
In Java:

Java virtual threads (Java 21+) enable lightweight async-like code

Step-by-Step Breakdown

1. CompletableFuture vs Promise

CompletableFuture is Java's equivalent of a Promise — it represents a value that will be available in the future.

JS
JavaScript
const result = await someAsyncOp();
JV
Java
CompletableFuture<String> f = CompletableFuture.supplyAsync(() -> someOp());
String result = f.get(); // blocks until done

2. Chaining

thenApply (transform result) and thenAccept (consume result) chain like JS .then().

JS
JavaScript
fetch(url).then(r => r.json()).then(data => console.log(data))
JV
Java
fetchAsync(url).thenApply(r -> parseJson(r)).thenAccept(System.out::println)

3. Virtual Threads (Java 21+)

Java 21 virtual threads let you write blocking code that scales like async — a major simplification over CompletableFuture.

JV
Java
Thread.ofVirtual().start(() -> {
    String data = fetchUser(1); // blocking but cheap
    System.out.println(data);
});

Common Mistakes

When coming from JavaScript, developers often make these mistakes:

  • JS is single-threaded with an event loop; Java uses real threads
  • JS async/await → Java CompletableFuture (Java 8+)
  • JS Promise.all → CompletableFuture.allOf
Common Pitfall
Don't assume Java works exactly like JavaScript. While the concepts may be similar, the syntax and behavior can differ significantly.

Key Takeaways

  • JS async/await → Java CompletableFuture
  • Promise.all → CompletableFuture.allOf
  • Java uses real threads; JS uses single-threaded event loop
  • Java 21 virtual threads simplify concurrent code
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your JavaScript code in Java to practice these concepts.
PreviousNext