Skip to main content

java-21

Java 21 language and runtime patterns for modern, safe code. Trigger: When writing Java 21 code using records, sealed types, or virtual threads.

소스 정보

저장소
Gentleman-Programming/Gentleman-Skills
최근 소스 활동
2026년 2월 2일 12:36
감지된 SKILL.md 언어
영어
스타
666
포크
99

설치 방법

기본적으로 소스를 먼저 확인하는 Prompt가 선택됩니다. 직접 명령으로 전환하거나 로컬 사본을 다운로드할 수도 있습니다.

소스 파일 검토

설치 여부를 결정하기 전에 SKILL.md와 SkillsMP에 표시된 보조 파일을 읽어 보세요.

SKILL.md 표시 중

SKILL.md
소스 지침 · 읽기 전용 미리보기
name
java-21
description
Java 21 language and runtime patterns for modern, safe code. Trigger: When writing Java 21 code using records, sealed types, or virtual threads.
metadata
{"author":"diegnghrmr","version":"1.0"}
## When to Use Load this skill when: - Writing Java 21 application or library code - Designing immutable DTOs or value objects - Modeling closed hierarchies with sealed types - Using virtual threads for blocking I/O ## Critical Patterns ### Pattern 1: Records for immutable data Use records for DTOs and value objects, validate in compact constructors. ### Pattern 2: Sealed types + pattern matching Use sealed interfaces/classes and switch pattern matching for exhaustiveness. ### Pattern 3: Virtual threads for I/O Use virtual threads to handle blocking I/O without large thread pools. ## Code Examples ### Example 1: Record with validation ```java package com.acme.user; public record Email(String value) { public Email { if (value == null || !value.contains("@")) { throw new IllegalArgumentException("Invalid email"); } } } ``` ### Example 2: Sealed hierarchy + switch pattern matching ```java package com.acme.payment; public sealed interface Payment permits Card, BankTransfer { } public record Card(String last4) implements Payment { } public record BankTransfer(String iban) implements Payment { } public final class PaymentPrinter { public String describe(Payment payment) { return switch (payment) { case Card card -> "card-" + card.last4(); case BankTransfer bank -> "iban-" + bank.iban(); }; } } ``` ### Example 3: Virtual threads for blocking calls ```java package com.acme.io; import java.util.concurrent.Executors; public final class Fetcher { public void fetchAll(java.util.List<String> urls) throws Exception { try (var executor = Executors.newVirtualThreadPerTaskExecutor()) { for (String url : urls) { executor.submit(() -> blockingFetch(url)); } } } private void blockingFetch(String url) { // perform blocking I/O here } } ``` ## Anti-Patterns ### Don't: Use mutable data carriers ```java // BAD: mutable DTO public class UserDto { public String name; public String email; } ``` ### Don't: Spin up raw platform threads per request ```java // BAD: expensive and unbounded new Thread(() -> blockingFetch("https://api" )).start(); ``` ## Quick Reference | Task | Pattern | |------|---------| | Immutable DTOs | Use records with validation | | Closed hierarchies | Use sealed interfaces + switch | | Blocking I/O scale | Use virtual threads executor | ## Resources - Java 21 Release Notes: https://www.oracle.com/java/technologies/javase/21-relnote-issues.html - JEP Index: https://openjdk.org/jeps/0
GitHub에서 보기