#Async in Java
1 messages ยท Page 1 of 1 (latest)
<@&1004656351647117403> please have a look, thanks.
Here is an AI assisted attempt to answer your question ๐ค. Maybe it helps! In any case, a human is on the way ๐. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.
In Java, especially with Spring MVC, asynchronous programming is typically handled differently than in languages with explicit async/await keywords (like JavaScript or C#). Spring MVC is primarily synchronous, blocking the thread until a response is ready. However, Spring Framework does support async processing through mechanisms like @Async, Callable, DeferredResult, or CompletableFuture.
WebFlux, on the other hand, is designed from the ground up for reactive, non-blocking async programming using Project Reactor (Mono, Flux). It supports highly concurrent, scalable apps by working on the event-loop model, unlike the traditional thread-per-request model in Spring MVC.
So while you donโt see async keywords, concurrency and async behavior in Java Spring MVC is achieved with these abstractions, but they are more verbose or explicit compared to async/await keywords. If your codebase is mostly Spring MVC, it likely does not leverage reactive WebFlux or reactive streams, hence the absence of async-style code there.
In summary:
- Java doesnโt have an
asynckeyword but offers async execution via@Async,CompletableFutureand callable results. - Spring MVC is synchronous by default but can be made async with concurrency abstractions.
- Spring WebFlux is the reactive, async framework in Spring, which you donโt see if the codebase uses MVC.
Useful links:
there are many ways to do multithreading in programming in general
the underlying base is always manual threading
yeah, and skip webflux
but people have built high level abstractions on top over the years
Use java 21+, spring boot mvc, and just enable virtual threads
a few common abstractions:
Is web flux not optimal?
not if you like your sanity ๐
lol
- async/promise
- future
- virtual threads
- coroutines
java opted for futures first. it has a rich future api (see ConpletableFuture as starting point)
and then added virtual threads recently, so thats what u have available for modern multithreading in java
But yeah, async/await are stackless coroutines, and virtual threads are stackful coroutines. First one is less overhead (hence why rust uses it), second one is way WAY easier to develop for (see Java with Virtual Threads and Go)
webflux/reactor is essentially legacy. the idea is interesting but the entire concept is very difficult to wrap ur head around and never really gained a lot of popularity. after virtual threads came it sorta lost its relevance
But The codebase mostly I've read does not have any async just mvc so that's why I am confused like that codebase is also from a company so how do they scale in real users. Maybe I missed the code which defined the async.
Are you talking about loom?
either case. if u want to do modern multithreading in java, either look at virtual threads and/or at CompletableFuture
latter has a nice api letting u write code like "do this. and then that. and then that. and then that..."
similar to the stream api
sure
its one of the best future apis ive seen across a few languages so far
But they still pollute every goddamn api like async/await does
indeed
We use them at work and... well
i know. tjbot is also full with it bc JDAs RestAction is essentially a future api
yeah, JDA is so friggin bloated it's insane
Lol I thinki async in Java is really complex than other language(don't know about c++). There is multiple ways and I am confused which to use. Cause to avoid over engineering.
First: what do you need threads for?
nah ur just not experienced enough on the topic yet
dont worry
its really not complex at all
Is it just web apps?
Or is it highly parallel processing of shared mutable data?
Or something in between
Huge range there
Yes. like I am making a amazon clone.
...so a web app?
Yes.
(c++ has a future api as well, but very barebones. and tries to go for limited coroutine support)
yeah, then keep it simple, use a framework that supports virtual threads (like spring boot) and just make shit ๐
yeah, its fully out of the box with spring
With standard WebMVC
u dont need to do touch any threads or multithreading at all with it
its all happening behind the scene
Yes I am using springboot. but is it async? not about threads like for io tasks.
Don't use webclient as well, use RestClient
every request u get is in its own thread. spring manages it
yes, every io call you make, in whatever shape or form, or any thread.sleep/park/... will be done completely asynchronously
But you can write the same exact code you would otherwise
So no need of using futures.
@tame narwhal read this first
Virtual threads are lightweight threads that reduce the effort of writing, maintaining, and debugging high-throughput concurrent applications.
To understand them
Then just enable them in spring boot
unless u want to compute multiple things in parallel for a single user flow. but again, could just make another virtual thread instead
spring already takes care for u that every user flow is async and doesnt block any other users requests
so u just have to think about it within a users flow
if u want to further parallelize long tasks within a single flow, so to speak
Could I have any example for like multiple things in parallel like in which use case.
I am final year at college, and I have like 5 month of free time to learn so I am just doing some random.
chances are, if what we say isnt immediately clicking for u, ur not deep enough in the topic that it would matter for u anyways. just go ahead and get started ๐
Yes its really good talking with experts most of the people I know does not know java and all ai is saying add web flux not giving definite answers.
ah yes, the clanker. It's surprisingly not well known
The amount of times I've had to explain how VTs work is, quite frankly, disturbing among my peers
For professional java devs
for java its also a relatively new thing. and most people dont have multilanguage experience to compare different multithreading approaches with each other
like coroutines vs virtual threads vs async vs futures and so on
or callbacks
(callback code is insanely annoying to work with)
and coroutinee are callbacks with sugar
however, the user interface is entirely different. and that's what counts
the term coroutines has been misplaced
Oh, I am very new to this java. So I am feeling like what should be I am aiming at like in ts I will be mainly trying to do some web apps, python mostly ml likewise.
yeah. ur choice is very good. for java ull likely end up looking at spring based backend positions
so that's what u should learn a bit about and practice
unless u have other plans
Yes. Is there any repo you would recommend me to study in github for java.
If you want to make web app. Be sure to separate the browser client app from the java server spring boot part.
The client web app should be its own static thing that load data in the browser.
You write servlet code and just enable virtual threads, that's the neat part