> java-concurrency
Modern concurrency patterns using Virtual Threads and Structured Concurrency. Use when implementing Java Virtual Threads, Structured Concurrency, or concurrent APIs. (triggers: **/*.java, thread, async, future, executor, synchronized, lock, await)
curl "https://skillshub.wtf/HoangNguyen0403/agent-skills-standard/java-concurrency?format=md"Java Concurrency
Priority: P1 (HIGH)
Modern concurrent programming emphasizing Virtual Threads (Loom) and safety.
Implementation Guidelines
- Virtual Threads (Java 21): Use for high-throughput I/O.
Executors.newVirtualThreadPerTaskExecutor(). - Structured Concurrency: Use
StructuredTaskScopeto treat related tasks as a single unit (Scope, Fork, Join). - Immutability: Share immutable data between threads to avoid race conditions.
- CompletableFuture: Use for composing asynchronous pipelines (if not using Virtual Threads).
- Atomic Variables: Use
AtomicInteger,LongAdderfor simple counters. - Locks: Prefer
ReentrantLock/ReadWriteLockoversynchronizedblocks for fine-grained control. - Thread Safety: Document
@ThreadSafeor@NotThreadSafe.
Anti-Patterns
new Thread(): Never manually create threads. Use Executors.- Thread Pooling Virtual Threads: Virtual threads are cheap; do not pool them.
- Blocking inside
synchronized: Pins the carrier thread (Loom pitfall). UseReentrantLock. - Shared Mutable State: The root of all concurrency bugs.
Thread.stop/suspend: Deprecated and dangerous.
Code
For full StructuredTaskScope and VirtualThread examples:
references/structured-concurrency.md
// Virtual Threads (Loom)
try (var scope = new StructuredTaskScope.ShutdownOnFailure()) {
var user = scope.fork(() -> api.fetchUser());
scope.join().throwIfFailed();
}
Related Topics
language | best-practices
> related_skills --same-repo
> typescript-tooling
Development tools, linting, and build config for TypeScript. Use when configuring ESLint, Prettier, Jest, Vitest, tsconfig, or any TS build tooling. (triggers: tsconfig.json, .eslintrc.*, jest.config.*, package.json, eslint, prettier, jest, vitest, build, compile, lint)
> typescript-security
Secure coding practices for TypeScript. Use when validating input, handling auth tokens, sanitizing data, or managing secrets and sensitive configuration. (triggers: **/*.ts, **/*.tsx, validate, sanitize, xss, injection, auth, password, secret, token)
> typescript-language
Modern TypeScript standards for type safety and maintainability. Use when working with types, interfaces, generics, enums, unions, or tsconfig settings. (triggers: **/*.ts, **/*.tsx, tsconfig.json, type, interface, generic, enum, union, intersection, readonly, const, namespace)
> typescript-best-practices
Idiomatic TypeScript patterns for clean, maintainable code. Use when writing or refactoring TypeScript classes, functions, modules, or async logic. (triggers: **/*.ts, **/*.tsx, class, function, module, import, export, async, promise)