Skip to content
Back to skills

Java Concurrency

ASecurity

The modern Java 21 approach is structured concurrency with virtual threads. ```java import java.util.concurrent.StructuredTaskScope; public class ApiService { private final RemoteClient client; public ApiService(RemoteClient client) { this.client = client; } public CombinedResponse fetchAll(String userId) throws Exception { try (var scope = new StructuredTaskScope.ShutdownOnFailure()) { StructuredTaskScope.Subtask<ResponseA> a = scope.fork(() -> client.fetchA(userId)); StructuredTaskScope.Sub...

  • 584 stars
  • 0 votes
  • 0 copies
  • 3 views
  • Added September 5, 2026
developmentjavaapi

Works with

  • cli
  • api

Security analysis

A100/100

Pro scans all 18 files and shows the line behind each finding

Scanned September 5, 2026

npx -y skills add HoangNguyen0403/agent-skills-standard --skill java-concurrency --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Java Concurrency?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Java Concurrency
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/hoangnguyen0403-java-concurrency-agent-skills-standard/badge)](https://www.skillsdirectory.com/skills/hoangnguyen0403-java-concurrency-agent-skills-standard)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
The modern Java 21 approach is structured concurrency with virtual threads.

```java
import java.util.concurrent.StructuredTaskScope;

public class ApiService {
    private final RemoteClient client;

    public ApiService(RemoteClient client) {
        this.client = client;
    }

    public CombinedResponse fetchAll(String userId) throws Exception {
        try (var scope = new StructuredTaskScope.ShutdownOnFailure()) {
            StructuredTaskScope.Subtask<ResponseA> a =
                    scope.fork(() -> client.fetchA(userId));
            StructuredTaskScope.Subtask<ResponseB> b =
                    scope.fork(() -> client.fetchB(userId));
            StructuredTaskScope.Subtask<ResponseC> c =
                    scope.fork(() -> client.fetchC(userId));

            scope.join();
            scope.throwIfFailed();

            return new CombinedResponse(a.get(), b.get(), c.get());
        }
    }
}
```

Why this is the preferred Java 21 style:

- `StructuredTaskScope` makes the 3 calls part of one unit of work.
- `ShutdownOnFailure` cancels sibling tasks if one call fails.
- `scope.fork(...)` runs each task concurrently.
- In Java 21, these subtasks are designed to work well with virtual-thread-based concurrency for I/O-heavy work.

If you want a timeout too:

```java
import java.time.Instant;
import java.util.concurrent.StructuredTaskScope;
import java.util.concurrent.TimeoutException;

public CombinedResponse fetchAllWithTimeout(String userId) throws Exception {
    try (var scope = new StructuredTaskScope.ShutdownOnFailure()) {
        var a = scope.fork(() -> client.fetchA(userId));
        var b = scope.fork(() -> client.fetchB(userId));
        var c = scope.fork(() -> client.fetchC(userId));

        boolean completed = scope.joinUntil(Instant.now().plusSeconds(2));
        if (!completed) {
            scope.shutdown();
            throw new TimeoutException("API calls timed out");
        }

        scope.throwIfFailed();
        return new CombinedResponse(a.get(), b.get(), c.get());
    }
}
```

If you cannot use preview structured concurrency yet, the fallback is:

```java
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public CombinedResponse fetchAllFallback(String userId) throws Exception {
    try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {
        Future<ResponseA> a = executor.submit(() -> client.fetchA(userId));
        Future<ResponseB> b = executor.submit(() -> client.fetchB(userId));
        Future<ResponseC> c = executor.submit(() -> client.fetchC(userId));

        return new CombinedResponse(a.get(), b.get(), c.get());
    }
}
```

So: for modern Java 21, prefer `StructuredTaskScope` for related parallel API calls, with virtual-thread-based execution underneath, and use `newVirtualThreadPerTaskExecutor()` as the non-structured fallback.

Files in this skill

  • eval-1.baseline.md1.6 KB
  • eval-1.with-skill.md2.8 KB
  • eval-2.baseline.md1 KB
  • eval-2.with-skill.md1 KB
  • eval-3.baseline.md1 KB
  • eval-3.with-skill.md1.4 KB
  • trigger-1.md172 B
  • trigger-10.md99 B
  • trigger-11.md192 B
  • trigger-2.md175 B
  • trigger-3.md204 B
  • trigger-4.md177 B
  • trigger-5.md172 B
  • trigger-6.md121 B
  • trigger-7.md116 B
  • trigger-8.md98 B
  • trigger-9.md164 B

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in development →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.