#Async in Java

1 messages ยท Page 1 of 1 (latest)

tame narwhal
#

Hi , Like in other language we use async if we want to be concurrent. but in java I didn't see any async like code something like webflux in the codebase. Mostly MVC. That's confusing me. is the services async or how does that code scale for real users. How does async work in java? Is it architectural pattern?

alpine quiverBOT
#

<@&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>.

#
TJ-Bot
Hi , Like in other language we use async if we want to be concurrent. but in java I didn't see any async like code something like webflux in the codebase. Mostly MVC. That's confusing me. is the services async or how does that code scale for real users. Ho

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 async keyword but offers async execution via @Async, CompletableFuture and 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:

thick bough
#

the underlying base is always manual threading

green fossil
#

yeah, and skip webflux

thick bough
#

but people have built high level abstractions on top over the years

green fossil
#

Use java 21+, spring boot mvc, and just enable virtual threads

thick bough
#

a few common abstractions:

tame narwhal
green fossil
#

not if you like your sanity ๐Ÿ™‚

tame narwhal
#

lol

thick bough
#
  • 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

green fossil
#

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)

thick bough
# tame narwhal Is web flux not optimal?

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

tame narwhal
#

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.

green fossil
#

loom IS virtual threads

#

a loom being the thing where you thread fibers etc

thick bough
#

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

green fossil
#

Eh

#

Debatable

thick bough
#

sure

thick bough
#

its one of the best future apis ive seen across a few languages so far

green fossil
#

But they still pollute every goddamn api like async/await does

thick bough
#

indeed

green fossil
#

We use them at work and... well

thick bough
#

i know. tjbot is also full with it bc JDAs RestAction is essentially a future api

green fossil
#

yeah, JDA is so friggin bloated it's insane

tame narwhal
#

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.

green fossil
#

First: what do you need threads for?

thick bough
#

nah ur just not experienced enough on the topic yet

#

dont worry

#

its really not complex at all

green fossil
#

Is it just web apps?

#

Or is it highly parallel processing of shared mutable data?

#

Or something in between

#

Huge range there

tame narwhal
#

Yes. like I am making a amazon clone.

green fossil
#

...so a web app?

tame narwhal
#

Yes.

thick bough
green fossil
#

yeah, then keep it simple, use a framework that supports virtual threads (like spring boot) and just make shit ๐Ÿ˜‰

thick bough
#

yeah, its fully out of the box with spring

green fossil
#

With standard WebMVC

thick bough
#

u dont need to do touch any threads or multithreading at all with it

#

its all happening behind the scene

tame narwhal
#

Yes I am using springboot. but is it async? not about threads like for io tasks.

thick bough
#

that's the beauty of virtual threads

#

yes, its fully async

green fossil
#

Don't use webclient as well, use RestClient

thick bough
#

every request u get is in its own thread. spring manages it

green fossil
#

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

tame narwhal
#

So no need of using futures.

green fossil
#

@tame narwhal read this first

#

To understand them

#

Then just enable them in spring boot

thick bough
#

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

tame narwhal
green fossil
#

you probably won't come across it for a while

#

So @thick bough let him cook first

tame narwhal
#

I am final year at college, and I have like 5 month of free time to learn so I am just doing some random.

thick bough
#

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 ๐Ÿ‘

tame narwhal
#

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.

green fossil
#

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

thick bough
#

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)

green fossil
#

well, virtual threads are coroutines ๐Ÿ™‚

#

so is async/await ๐Ÿ™‚

thick bough
#

and coroutinee are callbacks with sugar

green fossil
#

just a different flavor

#

nope, only some of them are

thick bough
#

however, the user interface is entirely different. and that's what counts

green fossil
#

the term coroutines has been misplaced

tame narwhal
#

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.

thick bough
#

so that's what u should learn a bit about and practice

#

unless u have other plans

tame narwhal
#

Yes. Is there any repo you would recommend me to study in github for java.

fervent crypt
pale briar