Merlin Bögershausen / @mboegie
class ExampleThread extends Thread {
public void run() { /* Do it */}
}
var t1 = new ExampleThread();
class ExampleRunable implements Runable {
public void run() { /* Do it again */}
}
var runable = new ExampleRunable();
var t2 = new Thread(runable);
t2.start(); t1.start();
t2.join(); t1.join();
ExecutorService
Thread Pool Management
reuse OS Threads
CompletableFuture
Composing asyncron operations
handling Results
Virtual threads are lightweight threads that improve Maintanace and Obervability.
Thread.ofVirtual().start(() -> doIOStuffInParallel());
Thread.ofPlatform() // is new as well
Executors.newVirtualThreadPerTaskExecutor()
.submit(() -> doIOStuffInParallel());
Lightweight?
Maintainabllility?
Oberservability?
Share immutable data between callees within a thread and child threads.
🥅 Ease of use
🥅 Comprehensibility
🥅 Robustness
🥅 Performance
final static ScopedValue<SSLContext> VAL
= ScopedValue.newInstance();
ScopedValue.where(VAL, value)
.run(() -> VAL.get());
ScopedValue.runWhere(VAL, value, () -> /* ... */);
private final static ScopedValue<Map<String, String>>
INPUTDATA = ScopedValue.newInstance();
private final static ScopedValue<SSLContext>
SSL_CTX = ScopedValue.newInstance();
ScopedValue.Carrier executionScope = ScopedValue
.where(INPUTDATA, Map.copyOf(data))
.where(SSL_CTX, SSLContext.getDefault());
// ScopedValue.Carrier executionScope;
for (var r : rules) {
executionScope.run(r::fire);
}
//ScopedValue<Map<String, String>> INPUTDATA;
String inputName = INPUTDATA
.orElseThrow(new IllegalArgumentException())
.getOrDefault("name", "JON");
// ScopedValue<SSLContext> SSL_CTX
if(SSL_CTX.isBound()) {
intputName = "%s %s".formatted(
inputName, getLastName(SSL_CTX.get()));
}
ScopedValues
Immutable Carrier
Same instance
Rebindable
ThreadLocal
Supplier
Instance per Thread
Mutable Value
Treat groups of related tasks running in different threads as a single unit of work, thereby streamlining error handling and cancellation, improving reliability, and enhancing observability.
public class StructuredTaskScope<T> implements AutoCloseable {
public <U extends T> Subtask<U>
fork(Callable<? extends U> task);
public StructuredTaskScope<T> join()
throws InterruptedException;
public StructuredTaskScope<T> joinUntil(Instant deadline)
throws InterruptedException, TimeoutException;
public void shutdown();
public void close();
}
ShutdownOnFailure first task failed
ShutdownOnSuccess first task successfully terminates
try (var scope = new StructuredTaskScope.ShutdownOnFailure()){
Supplier<String> adr = scope.fork(() -> readAddress());
scope.join().throwIfFailed();
var addres = adr.get();
} catch (ExecutionException | InterruptedException e) {
throw new RuntimeException(e);
}
Never pool VirtualThreads
Migrate immediately
ThreadLocal never mutated ⇒ ScopedValues
IO Concurrency ⇒ StructuredConcurrency
GitHub GIST with full code
Knited Speach Blubled generetd with deepai.org
Virtual reality background generetd with deepai.org
Elderly Person handing over treats generated with deepai.org
Chaos of pipes and notations generated with deepai.org