Skip to content

Instantly share code, notes, and snippets.

@jexp
Created March 22, 2023 15:02
Show Gist options
  • Save jexp/220051816ba8a305872c4f1487a13d72 to your computer and use it in GitHub Desktop.
Save jexp/220051816ba8a305872c4f1487a13d72 to your computer and use it in GitHub Desktop.
// sdk install java 20-open
// java -version
/*
openjdk version "20" 2023-03-21
OpenJDK Runtime Environment (build 20+36-2344)
OpenJDK 64-Bit Server VM (build 20+36-2344, mixed mode, sharing)
*/
// jshell --enable-preview --add-exports java.base/jdk.internal.vm=ALL-UNNAMED --add-modules=jdk.incubator.concurrent
// Java Threads & Thread Pools
var t = new Thread(() -> System.out.println("Hello World"));
t.start();
t.join();
// new! Thread.Builder
var t = Thread.ofPlatform().start(
() -> System.out.println("Hello new World"));
t.join();
// Thread Pools - Platform Threads
try (var executor = Executors.newFixedThreadPool(5)) {
IntStream.range(0, 50).forEach(i -> {
executor.submit(() ->
System.out.println("Hello Platform Thread "+i+" "+Thread.currentThread())
);
});
} // executor.close()
// takes a long time
import java.time.*;
long start = System.currentTimeMillis();
try (var executor = Executors.newFixedThreadPool(10)) {
IntStream.range(0, 100).forEach(i -> {
executor.submit(() -> {
Thread.sleep(Duration.ofSeconds(1));
return i;
});
});
} // executor.close()
long duration = System.currentTimeMillis() - start;
// 10000 ms
// Parallel Streams
// parallel stream
IntStream.range(1,10).parallel()
.mapToObj( i -> "Hello World "+i)
.forEach(System.out::println);
// Completable future
var cf = CompletableFuture.completedFuture("complex")
.thenApplyAsync(String::toUpperCase)
.thenCombine(
CompletableFuture.completedFuture("CODE")
.thenApplyAsync(String::toLowerCase),
(s1, s2) -> s1 + s2);
cf.join()
// Java Virtual Threads - Builder
var threads =
IntStream.range(0,10).mapToObj(i ->
Thread.ofVirtual().start(() -> {
System.out.println("Hello Virtual Thread "+i+" "+Thread.currentThread());
})).toList();
for (Thread t : threads) {
t.join();
}
// lots of threads
long start = System.currentTimeMillis();
try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {
IntStream.range(0, 1_000_000).forEach(i -> {
executor.submit(() -> {
Thread.sleep(java.time.Duration.ofSeconds(1));
return i; // callable, throws Exception
});
});
} // executor.close() is called implicitly, and waits
long duration = System.currentTimeMillis() - start;
// 3900 ms
java --enable-preview --source 20 LoomServer.java
echo -n 'Hello Loom' | nc -n 127.0.0.1 2000
import jdk.internal.vm.Continuation;
import jdk.internal.vm.ContinuationScope;
var scope = new ContinuationScope("scope");
var c = new Continuation(scope, () -> {
System.out.println("Started");
Continuation.yield(scope);
System.out.println("Running");
Continuation.yield(scope);
System.out.println("Still running");
});
System.out.println("Start");
int i=0;
while (!c.isDone()) {
c.run();
System.out.println("Running "+i+" result "+c.isDone());
i++;
}
System.out.println("End");
import jdk.incubator.concurrent.*;
try ( var scope = new StructuredTaskScope.ShutdownOnSuccess<String>() ) {
IntStream.range(0,10).forEach(i ->
scope.fork(() -> String.valueOf(i)));
scope.join();
// first returning wins, exception if none did
System.out.println(scope.result());
}
import jdk.incubator.concurrent.*;
private static final ScopedValue<String> VERSION = ScopedValue.newInstance();
Runnable runnable = () -> System.out.println(VERSION.get());
ScopedValue.where(VERSION, "Java 19", () ->
System.out.println(VERSION.get()));
var s = ScopedValue.where(VERSION, "Java 20");
s.run(runnable); // Java 20
s.where(VERSION, "Java 21").run(runnable); // Java 21
s.run(runnable); // Java 20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment