Skip to content

Instantly share code, notes, and snippets.

@DasBrain
Last active July 3, 2020 18:17
Show Gist options
  • Save DasBrain/2aebc453894db682d92fe78ae0cf56e2 to your computer and use it in GitHub Desktop.
Save DasBrain/2aebc453894db682d92fe78ae0cf56e2 to your computer and use it in GitHub Desktop.
package pw.dasbrain.test.loom;
import java.util.concurrent.ForkJoinPool;
public class CancelVT {
public static void main(String[] args) {
Runnable r = () -> {
try {
System.out.println("Running");
Thread.sleep(500);
} catch (InterruptedException e) {
System.out.println("Interrupted");
} finally {
System.out.println("Finally");
}
};
try (var pool = new ForkJoinPool()) {
Thread.builder().virtual(pool).task(r).start();
// Assume more virtual threads are started in that pool, one completes after 250ms.
Thread.sleep(250);
System.out.println("shutdown pool");
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("done");
}
}
Running
shutdown pool
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment