Skip to content

Commit

Permalink
JEP 444: Virtual Threads
Browse files Browse the repository at this point in the history
  • Loading branch information
ibrahimatay committed Nov 17, 2023
1 parent 7a3efc5 commit 04791a0
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ This repository contains Java examples that are designed to track and document t
* [Java 21](java-21) (September, 2023)
* [JEP 430](https://openjdk.org/jeps/430): String Templates
* [JEP 431](https://openjdk.org/jeps/431): Sequenced Collections
* [JEP 444](https://openjdk.org/jeps/444): Virtual Threads

* [Java 16](java-16/) (March, 2021)
* [JEP 395](https://openjdk.java.net/jeps/395): Records
Expand Down
54 changes: 54 additions & 0 deletions java-21/src/main/java/com/ibrahimatay/JEP444VirtualThreads.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.ibrahimatay;

import java.util.concurrent.Executors;
import java.util.stream.IntStream;

/*
* JEP 444: Virtual Threads
* https://openjdk.org/jeps/444
* */
public class JEP444VirtualThreads {
public static void main(String[] args) {
Runnable fn = () -> {
IntStream.range(0, 100_000).forEach(i-> {
System.out.println(i);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
});
};

// Platform Threads
new Thread(fn).start();

Thread.ofPlatform().start(fn);
Thread.ofPlatform().daemon().name("my-custom-thread").unstarted(fn);

// Virtual Threads

Thread.startVirtualThread(() -> {
IntStream.range(0, 100_000).forEach(i-> {
System.out.println(i);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
});
});

var executorService = Executors.newVirtualThreadPerTaskExecutor();
executorService.submit(() -> {
IntStream.range(0, 100_000).forEach(i-> {
System.out.println(i);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
});
});
}
}

0 comments on commit 04791a0

Please sign in to comment.