Luigit
repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by admin

pi/agent/skills/optional/java/java-virtual-threads/examples/jep-444.md

Raw
Rendered preview

JEP 444 / JEP 491 examples

Sources: https://openjdk.org/jeps/444 and https://openjdk.org/jeps/491

try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {
    Future<String> a = executor.submit(() -> fetch(uriA));
    Future<String> b = executor.submit(() -> fetch(uriB));
    System.out.println(a.get() + b.get());
}
// Use resource limits, not virtual-thread pools.
Semaphore dbSlots = new Semaphore(20);
Thread.startVirtualThread(() -> {
    dbSlots.acquireUninterruptibly();
    try { queryDatabase(); }
    finally { dbSlots.release(); }
});
# [JEP 444](https://openjdk.org/jeps/444) / [JEP 491](https://openjdk.org/jeps/491) examples

Sources: https://openjdk.org/jeps/444 and https://openjdk.org/jeps/491

```java
try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {
    Future<String> a = executor.submit(() -> fetch(uriA));
    Future<String> b = executor.submit(() -> fetch(uriB));
    System.out.println(a.get() + b.get());
}
```

```java
// Use resource limits, not virtual-thread pools.
Semaphore dbSlots = new Semaphore(20);
Thread.startVirtualThread(() -> {
    dbSlots.acquireUninterruptibly();
    try { queryDatabase(); }
    finally { dbSlots.release(); }
});
```