0

My application is using Quarkus, but not taking advantage of Mutiny / reactive programming yet. It's not realistic to re-implement everything at this time, I'm considering using it in certain places (like the quarkus reactive sql client, or MicroProfile REST calls to a 3rd party).

If only part of my code is async, and then I need to await() a response from the database or 3rd party REST call, does that completely defeat the purpose? Does my original thread just wind up blocking and being held up while awaiting, or is there some magic going on that frees up that thread until await() returns?

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953

1 Answers1

1

Without Virtual Threads, await() blocks the caller thread until the underlying operation completes. As you already suspected, this would defeat the purpose of freeing threads from waiting for I/O to happen. However, as @Clement pointed out, if you have Virtual Threads support (from Java 21 onwards), then await() will only block the virtual thread, effectively freeing the platform thread from waiting.

In general, if you want to take advantage of asynchronous or reactive programming with Mutiny (or any similar API), then you'll have to use Uni or Multi all the way throughout your codebase. This changes with Virtual Threads. Then you can use an imperative API like await() without blocking system threads.

Oliver Marienfeld
  • 1,154
  • 9
  • 17
  • 2
    `await` got designed with virtual threads in mind. So, when you use virtual threads, using `await()` will be really convenient: 1) you get clients using non-blocking I/O, super efficient... 2) you use await and your code looks simpler. – Clement Jul 12 '23 at 07:59
  • I hadn't heard about virtual threads before, Quarkus has some good information and examples documented - https://quarkus.io/guides/virtual-threads – Jesse V Jul 12 '23 at 13:07
  • Great info @Clement! So, Mutiny will choose Virtual Threads automatically if available? – Oliver Marienfeld Jul 13 '23 at 10:11
  • Mutiny does not create threads - but when you are using virtual threads, await() is really convenient (and actually quite performant). – Clement Jul 16 '23 at 17:09