0

The Java HttpClient can send either a synchronous or an asynchronous request. The synchronous method blocks until a response has been received, while the asynchronous does not. But what does this mean for a developer? Generally speaking, I assume that asynchronous should be more efficient. Yet, the fact that synchronous requests exist, means that there are usages for them as well. So the question is: When should I send a synchronous request and when should I send an asynchronous? My assumption is there is no specific rule on this but there should be some well-known cases, when one is a particularly better option than the other.
In my current case, I am trying to write an app for searching articles from a web API, which has paging. However, my question is not just about this particular case. I'd like to receive some general tips on how to chose one of the two approaches, as well.

Kotaka Danski
  • 451
  • 1
  • 4
  • 14
  • 2
    Always use a synchronous call, unless you have a specific reason not to. It’s simpler and easier to follow. An asynchronous call might make sense when you want to make sure the current thread does not get held up due the speed of the network, such as when making a call from a Swing and JavaFX application. – VGR Jan 23 '22 at 01:46
  • @VGR All right. A synchronous call is indeed so much easier to implement. Especially since my task is to provide an API, not a GUI or Console app, I guess it would really be an overkill. Thanks. – Kotaka Danski Jan 23 '22 at 07:13

0 Answers0