-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7a3efc5
commit 04791a0
Showing
2 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
java-21/src/main/java/com/ibrahimatay/JEP444VirtualThreads.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}); | ||
}); | ||
} | ||
} |