Java 21+ Modern Features & Virtual Threads
When to use
Use this skill when you need to developing scalable Java applications using Project Loom virtual threads, pattern matching, and record patterns.
Prerequisites
- Working knowledge of relevant programming language or database fundamentals.
- Access to testing environment or local runtime instance.
Step-by-step workflow
- Analyze operational requirements: Review performance boundaries, data integrity constraints, and query patterns.
- Design schema / interface: Define explicit data contracts, types, or indexes supporting planned operations.
- Implement core routines: Construct declarative, type-safe, or parameterized queries and functions.
- Validate performance: Verify execution metrics using profilers, EXPLAIN plans, or benchmark test suites.
- Implement defensive controls: Enforce timeout boundaries, connection pooling limits, and transaction safety.
Commands / code examples
import java.util.concurrent.Executors;
import java.util.stream.IntStream;
public class VirtualThreadDemo {
public static void runWorkload() {
// High-throughput virtual thread executor
try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {
IntStream.range(0, 1000).forEach(i -> {
executor.submit(() -> {
Thread.sleep(100);
return i * 2;
});
});
} // Automatically joins on close
}
public sealed interface Result permits Success, Failure {}
public record Success(String payload) implements Result {}
public record Failure(String error) implements Result {}
}
Common mistakes & pitfalls
- Overlooking indexing strategies on foreign keys and frequently queried filter columns.
- Running unparameterized dynamic queries, creating severe SQL injection vulnerabilities.
- Holding open long-running database transactions during slow external network calls.
Verification checklist
- Schema contracts and type signatures are strictly enforced.
- Queries are fully parameterized against injection vulnerabilities.
- Profiling or execution plans show optimal resource consumption.
- Error handling safely releases locks and rolls back aborted transactions.
Related skills
- postgresql-query-optimization-explain
- sql-anti-patterns-remediation
- acid-transactions-isolation-levels
- secure-code-review-static-analysis-sast