0

I have a backend app in Java that process Images and PDFs. When I process the PDF the app needs a few seconds so there is not problem. When I need to process image, in order to get the best result I run other processes that takes time (like 1 or 2 minutes) and the idea is that the user can process several documents at the same time, so it could take really long!

So in order to avoid the user to keep waiting for the app to finish the process, I implemented Thread (Runnable) in order to generate a new Thread for each document. But as the process is the same (after the image process), the method is share between both file types.

So I want to generate new Thread, but for the PDF files, I want that the user do not notice any change, I mean, that the app process the file and return a response to the front end. But for the image, I wanted to create new thread and keep working on the background while the user see a message saying that the files are being processed, and he will be notify when the process ends.

So when I was checking this answer: How can I pass a parameter to a Java Thread?

But then instread of:

Runnable r = new MyRunnable(param_value);
Thread t = new Thread(r).start();

I want to have like

if(type = "PDF"){
t.join();
response = getSomeResponse();
}
else{
//ShowMessage
}

So, is there a way to work with threads, and for some cases wait for it to finish and get a result and in some cases just let them work while the main thread is complete and I can inform the user that he will be notify at the end?

Faabass
  • 1,394
  • 8
  • 29
  • 58
  • Ok, but you seem to have forgotten to ask a question. What is it you want to know? – John Bollinger Aug 01 '18 at 21:30
  • Sorry! I added now! – Faabass Aug 01 '18 at 21:35
  • For waiting for a result: Submit a [`Callable`](https://docs.oracle.com/javase/10/docs/api/java/util/concurrent/Callable.html) to an [`ExecutorService`](https://docs.oracle.com/javase/10/docs/api/java/util/concurrent/ExecutorService.html) and call `get()` on the returned [`Future`](https://docs.oracle.com/javase/10/docs/api/java/util/concurrent/Future.html). For being notified whenever the task completes (without waiting): See [`CompletableFuture`](https://docs.oracle.com/javase/10/docs/api/java/util/concurrent/CompletableFuture.html). – Slaw Aug 01 '18 at 21:40
  • Also see https://stackoverflow.com/questions/9148899/returning-value-from-thread and https://stackoverflow.com/questions/826212/java-executors-how-to-be-notified-without-blocking-when-a-task-completes – Slaw Aug 01 '18 at 21:44
  • Yes, there are ways, but if you're looking for a *discussion* on that topic then your question is inappropriate for SO. Do you have a specific question about how to accomplish it? Is there a particular aspect of the problem on which you are hung up? – John Bollinger Aug 01 '18 at 21:48
  • Yes, I want a solution. I describe my request. So I’m expecting a way of solving that requirement. For executors, I don’t get how to describe when I need to wait for result and how to get it, and when I do not have to wait – Faabass Aug 01 '18 at 21:52
  • Why would you want to handle PDFs and images differently? I don't see a reason for that. If the handling of PDFs is faster, the user could just see the "processing" message for a shorter period of time (or you could have an `n` second threshold for showing the message in the frontend). It seems that you just want a generic solution for background parallel processing. There are lots of tutorials out there for the built in JDK classes such as `ExecutorService`. [Here's one](http://www.baeldung.com/java-executor-service-tutorial). – Mick Mnemonic Aug 01 '18 at 22:45
  • I’m saying that the image processing takes like 20 minutes (depending on the amount of images) so i cannot hace the user waiting that amount of time. The image processing is low because of the transformation I need to do.. – Faabass Aug 01 '18 at 23:01
  • I'm guessing your goal is to improve the UX, rather than to have the user waiting for 20 minutes. At least from my experience, it's best to write into a folder/db; then either the frontend get's a pull/push to server to retrieve the needed file. You might not need a response thread as no users will wait for 20 minutes. – Han Aug 02 '18 at 00:39
  • @MickMnemonic to store in a dB first I need the process that take 20 minutes.. for that reason I need the thread that I’m asking from the begging – Faabass Aug 02 '18 at 02:26

0 Answers0