0

I want create 2 threads and run them both at the same time. I want put condition that if the first thread answer data null or empty I want set the other thread response (it can be null or empty I don't need put restriction). How can I do it, any suggestions? I want it to do in Spring Framework.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • 2
    It is not clear to me, what you try to achieve and especially what it has to do with Spring. Maybe [this](https://stackoverflow.com/a/2314575/9107502) answer will help you. – 0x1C1B Sep 02 '22 at 08:33
  • I want do it in Spring thats why I especially tagged Spring, I want to achieve that there are two feign client calling in one controller and I want it call 2 feign clients "same " time with Threads and after that if first Thread have response set it to response , if not second Thread answer will set to response. – Elbrus Hasanzade Sep 02 '22 at 10:45

2 Answers2

0

You could set an environment variable and if not a global variable maybe stuff into a control file that each thread could read. Or even a data base entry.

Skint
  • 1
  • 2
  • I call the feign client for getting data . I want call "same" time them and put condition if the first Thread have no data set the other Thread answer . – Elbrus Hasanzade Sep 02 '22 at 08:08
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 07 '22 at 06:24
0

you could try CompletableFuture, e.g.

CompletableFuture<String> future1  
  = CompletableFuture.supplyAsync(() -> firstApi());
CompletableFuture<String> future2  
  = CompletableFuture.supplyAsync(() -> secondApi());

CompletableFuture<Void> combinedFuture 
  = CompletableFuture.allOf(future1, future2);

// wait for completion
combinedFuture.get();

// check and return results, e.g.
String result1 = future1.get();
if(null == result1) {
  return future2.get();
}
return result1;

https://www.baeldung.com/java-completablefuture

Marc Stroebel
  • 2,295
  • 1
  • 12
  • 21