38
50
17
30
18
u/emaphis Nov 15 '21
So is Virtual Threads looming for JDK 18?
4
10
u/henk53 Nov 15 '21
Haha, unlikely. Hopefully before JDK 22 or so ;)
9
9
8
u/sureshg Nov 16 '21
Have been waiting for this...☕🚀🚀🚀 kudos to Ron, Alan and whole team for all the amazing work.
7
15
u/chuggid Nov 16 '21
"ofVirtual()"? "ofPlatform()"? These are strange, mealy-mouthed method names. I assume there was some good reason for nixing something like newVirtualThread() and newPlatformThread() which naïvely seem nicer and are statically importable.
8
u/jrh206 Nov 16 '21
Yes, I hope they change those names. I can see why they don’t want to use “new” because they want a fluid builder interface. But please not this “ofVirtual” framing, it doesn’t make any sense.
3
Nov 16 '21
Isn't "of" supposed to be used when you're writing a method that's meant to convert one type to another? If so, it does sound confusing. There's no virtual thread argument being converted to something else.
4
u/pron98 Nov 16 '21 edited Nov 16 '21
Go to the current Javadoc, type
.ofinto the search box and see how many methods follow this pattern.6
u/Necessary-Conflict Nov 16 '21
The name "of" was suggested in the Effective Java book, right in Item 1, for "An aggregation method that takes multiple parameters and returns an instance of this type that incorporates them, for example Set<Rank> faceCards = EnumSet.of(JACK, QUEEN, KING);"
More recently the name ofSomething was also used for converting static factory methods like Duration.ofDays(long days), this also makes sense.
But in this case the "of" just isn't adding anything to the readability of the code.
BTW the "platform" part of ofPlatform is also questionable, because not just the operating system is a "platform", but Java is a "platform" as well (see the Javadoc's title: "Java® Platform, Standard Edition & Java Development Kit Version 18 API Specification").
1
u/pron98 Nov 16 '21
because not just the operating system is a "platform", but Java is a "platform" as well
Yes, and that's intentional. The Java specification does not require that platform threads are actually OS threads.
3
u/chuggid Nov 16 '21
Several comments:
Do you like the pattern?
The only
ofI see injava.langproper is inUnicodeBlock. I also note noofSomethingmethods anywhere as creation verbs but I didn't do an exhaustive search.Go to the current javadoc, type
.createinto the search box and see how many methods follow this pattern.1
u/pron98 Nov 16 '21
I also note no ofSomething methods
There are lots of ofSomethings. Anyway, we're talking about methods whose names people will rarely write or see, so I'll pass along your concern, but I don't think it merits much of a debate.
8
u/chuggid Nov 16 '21
But at least they all take arguments, right, and scan OK if you prefix them with "give me a new
DeclaringClass…"?That is:
Set.of(foos)means "give me a newSetoffoos"; if you squint a bitSet.of()means "give me a newSetof nothin'";Duration.of(something)means "give me a newDurationofsomething"; and so on and so on and presumably that's howofrose from the primordial slime in the first place, since you can't name a methodnew()orfor(), and presumably someone thoughtcreate()was too long or verbose or something for some reason.
Thread.ofPlatform()means "give me a newThreadof platform" by the same logic, which is nonsensical, andThread.ofVirtual()means "give me a newThreadof virtual", which…virtual isn't even a noun.These Loom bits may indeed be methods that "people will rarely write or see" but they are in
java.lang, not, say,java.lang.constant, so I think the additional scrutiny is worthwhile, and a debate is merited, and I'm not (apparently) the only one with these concerns. I do understand you disagree, and think that these methods are suitably named. Hopefully my feedback is not DOA, but, whether it is or not, it would be nice to know how exactlyofVirtual()in particular came to be, if only for bar trivia purposes! :-)
2
u/nimtiazm Nov 16 '21
Any server benchmarks available? Specially compared to reactive solutions.
2
u/HxA1337 Nov 17 '21
Yes that would be really interesting. What can we expect here when we switch a Servlet based Web app to run on virtual threads instead of a native thread pool.
3
u/rubydesic Nov 16 '21
Will the Continuation/ContinuationScope API or something with that functionality ever be made public?
Also are generators planned?
0
1
1
u/Dokiace Nov 16 '21
Is it too optimistic thinking that this will be in the next LTS?
1
u/TheCountRushmore Nov 16 '21
There is a chance.
Possible preview in 19, 20, 21 and final in 22.
I think it all depends on the feedback they get and the changes necessary after the first preview and even better these EA builds.
2
u/jazd Nov 19 '21
So basically, the sooner we all test it and the more feedback we give the more likely it will be.
1
u/TheCountRushmore Nov 19 '21
Yes. But they do want real examples of real problems. Not something theoretical you thought up while reading the JEP. They want you to write and run code.
1
u/jazd Nov 19 '21
Yeah I have been meaning to try running up our application on the ea and try the features out.
1
0
0
Nov 16 '21
[deleted]
6
u/pron98 Nov 16 '21
[Executors.newVirtualThreadPerTaskExecutor()](https://download.java.net/java/early_access/loom/docs/api/java.base/java/util/concurrent/Executors.html#newVirtualThreadPerTaskExecutor()) is still there, and is a very good migration path.
You might be referring to custom schedulers, that don't map tasks to virtual threads and work on top of them, but control how virtual threads themselves are implemented (so it's a mechanism below virtual threads). Custom schedulers most certainly do not assist migration; they are a super-specialised mechanism intended to right specific, niche frameworks (e.g. those that interact with the graphics card) using virtual threads. We do intend to make those public (although fewer than 0.1% of users would want to use them directly), but we haven't yet had the time to fully review that API, so we're leaving it out for now until we're more confident about it.
1
u/pronuntiator Nov 16 '21
I haven't heard of wrapping executors, but you can replace them by their virtual counterpart (Executors.newVirtualThreadPerTaskExecutor()), for example in Spring @Async. The global scheduler is created by default (just like the one for Stream.parallel()).
39
Nov 16 '21
[removed] — view removed comment
11
u/lechnito Nov 16 '21 edited Nov 16 '21
Do you mind sharing what you don't like about the current state of reactive programming in java?
I too have clawed my way up the reactive java learning curve, which for me was particularly steep because I came from a strictly imperative background. But now that I have a few projects under my belt in Reactor, there are aspects of it that I definitely prefer.
For your projects, do you think you would you be able to hit the ground running once Loom becomes generally available or would you wait for some framework that utilizes the new virtual threading model?
6
u/pronuntiator Nov 16 '21
I've never used reactive Java, but from using rxjs in Angular I can only imagine the mind twists you have to do to perform simple joins and maps. Also, how pervasive is it? Do Mono and Flux bubble all the way from a foreign API call through your business code up to your controller?
8
5
u/lechnito Nov 16 '21
Ha! It's true that implementing simple things can be tricky in reactor. There are composition patterns that help but it feels more of an art than science sometimes.
Do Mono and Flux bubble all the way from a foreign API call through your business code up to your controller?
Yes and no...
Yes because with spring webflux for example on some level your application is a chain of reactive operations that gets a subscriber when a request arrives. It doesn't matter how many levels of async operations are below the subscriber, webflux calls your application code reactively.
No because you definitely want to hide reactive constructs like Mono and Flux from core business logic where ever possible. If I needed to call a remote API, I would use spring webflux's
WebClientand pass the results into map() if I needed to do something imperative or flatMap() if async.24
Nov 16 '21
[removed] — view removed comment
7
u/lechnito Nov 16 '21
Fair points all around!
I think Reactor is hard for beginners and for really anyone who has a traditional java background. The more experience you have with async concepts & functional programming languages, the easier go you will have.
Readable & maintainable unit tests remains challenging. StepVerifier only became readable after I started using the junit5
@ParameterizedTeststuff and writing custom test interfaces with functional helper methods. Honestly, I have started replacing most of the unit tests that involve reactive features with WebTestClient based integration tests just to make life easier.Getting SLF4J's MDC to work for a homegrown distributed tracing service in Reactor has been a huge issue too. Sometimes I worry that the performance cost of using Hooks & contextWrite() offsets any gains from Reactor.
I'm still wrapping my head around Loom, but I think my initial take is that I would miss some of compositional improvement that accompany reactive streaming. Like operating over streams of T instead of imperative loops for instance.
4
u/MR_GABARISE Nov 16 '21
I'd wager that a proper MDC in Loom would certainly aim to make use of Scope Locals, and it's not even guaranteed that they will ship together.
1
6
3
u/private_static_int Nov 16 '21
Yup, Developer Experience in Reactive Programming goes to shit. Project Reactor and Spring Webflux, allthough technically impressive, are a fucking nightmare of chained zip/map/flatMap calls and low level programming (dataBuffers, khe khe).
It should die and be forgotten by everyone.
11
u/blobjim Nov 16 '21
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.