Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use multithreading to make the same rest call mutiple times at once?

I am using java and going to do some rest calls using Zephyr's api. I don't think the the api used will affect the possibility of my question. I was just wondering if it is possible make the same rest call multiple times at the same time using multithreading? Each call would be retrieving different data, they are not all grabbing the same data.

This is just strictly for retrieving data, not writing.

If this is possible, what are the risks? Is this recommended?

like image 516
simhuang Avatar asked Dec 20 '22 05:12

simhuang


1 Answers

For sure, it will improve your performance if you implement it the right way. As mentioned, you just need to be careful with the implementation to avoid security and performance issues. I'd suggest to have a thread pool, so you can manage many threads that are performing the REST calls you're doing at the same time.

ThreadPoolExecutor will be helpful.

You can use Executors.newFixedThreadPool/Executors.newCachedThreadPool. Both of them works fine.

You can create a task (implementing Callable) to perform your REST invokation or call, and then use the invokeAll() of the ThreadPoolExecutor (You would like to include the list of the tasks here).

Hope it helps you.

Best regards.

like image 61
Marcelo Tataje Avatar answered May 20 '23 13:05

Marcelo Tataje