1

We are required to call an HTTP endpoint but our main process is restricted to be single-threaded (we are running multiple instances of the same process and we don't have enough resource on the machine).

Is it possible to do a fire and forget to do it? Options I always see (which I understand and appreciate) are ExecutorService, @Async or other approaches that essentially creates a new thread (or uses an existing one).

Is this doable?

Rey Libutan
  • 5,226
  • 9
  • 42
  • 73
  • 2
    No. A thread can do only one thing at a time. – Jorn Jun 22 '23 at 14:47
  • Does timing-out out of the HTTP request early (like after 5 seconds) even help here? – Rey Libutan Jun 22 '23 at 14:49
  • 1
    you could use nonblocking http clients, including a built-in in java `HttpClient`, however they would use auxiliary thread under the hood – gdomo Jun 22 '23 at 14:50
  • 3
    @Jorn That's [only partially accurate](https://stackoverflow.com/questions/14177891/understanding-async-await-in-c-sharp). While threads are technically only doing one thing at a time, asynchronous programming allows threads to do other tasks while they're waiting for slow external processes (such as network calls). – EJoshuaS - Stand with Ukraine Jun 22 '23 at 14:51
  • @EJoshuaS I don't see how that's relevant to the question. You either block your main thread or you use a second thread. – Jorn Jun 22 '23 at 15:04
  • @Jorn That's not true at all. Plenty of languages offer non-blocking I/O and network calls, which typically don't require the creation of additional threads. Please see the article I link to for an explanation of why C#'s async calls don't require additional threads. Java admittedly doesn't appear to have particularly great support for this, but that's not an accurate statement in the general case. – EJoshuaS - Stand with Ukraine Jun 22 '23 at 15:07
  • 3
    *our main process is restricted to be single-threaded* Has anyone involved in coming up with that restriction ever counted the number of threads any instance of `java` has? Adding one more to that isn't going to change much. – Andrew Henle Jun 22 '23 at 15:09
  • Do you need GET or POST? – gdomo Jun 22 '23 at 15:19
  • @gdomo, we are POSTing some data but we don't care about the results. – Rey Libutan Jun 22 '23 at 15:21
  • @EJoshuaS If you are talking about Project Loom's Virtual Threads... I still say that's out of scope for this question. Don't make thing more difficult than they need to be. Even if you are right about what *can* be done, it doesn't help OP. As for non-blocking IO... there's always *something* that waits for it to complete. Whether that's a full thread, virtual thread, coroutine or something else doesn't really matter. – Jorn Jun 22 '23 at 15:31

1 Answers1

1

If you don't need an answer you could open connection and synchronously send your data without waiting for response. For example, with basic built-in java URLConnection:

HttpURLConnection urlConnection = (HttpURLConnection) new URL(urlString).openConnection();
urlConnection.setDoOutput(true);
urlConnection.setRequestMethod("POST");
urlConnection.setRequestProperty("Header-name", "header value");
try (OutputStream os = urlConnection.getOutputStream()) {
    os.write("any payload".getBytes());
    os.flush();
}
try (InputStream ignored = urlConnection.getInputStream()) {
}
urlConnection.disconnect();

It wouldn't wait for response, however sending the data could also take a while. More information on URLConnection here and there. Also, be cautious, it's up to the server how it would handle premature connection close, which would happen if you don't wait for the answer.

gdomo
  • 1,650
  • 1
  • 9
  • 18