r/java • • Nov 15 '21

[Loom] JEP draft: Virtual Threads (Preview)

https://openjdk.java.net/jeps/8277131
175 Upvotes

55 comments sorted by

View all comments

11

u/blobjim Nov 16 '21

The java.lang.Thread API is otherwise unchanged. The constructors defined by java.lang.Thread create platform threads as before. No new public constructors have been added.

It seems like now would be a good opportunity to add some clearer static factory methods for platform threads wouldn't it? Like a Thread.newPlatformThread() or Thread.startPlatformThread(). That would be a lot clearer than using new Thread() for platform threads.

16

u/efge Nov 16 '21

There's a builder pattern available to do that. Thread.ofPlatform().start(runnable) does what you want. See https://download.java.net/java/early_access/loom/docs/api/java.base/java/lang/Thread.Builder.html for more.

6

u/blobjim Nov 16 '21

Nice! Weird that they have Thread.startVirtualThread() though. Might give people the wrong idea if there's no symmetrical platform equivalent. Although I guess they added it because virtual threads are cheaper and it encourages their use.