Can I use multithreading to call the same call at the same time?

I am using java and am about to make some rest calls using Zephyr api. I don't think the api used will affect the possibility of my question. I'm just wondering if it is possible to use the same call at the same time to be called again using multithreading? Each call will retrieve different data, they do not all grab the same data.

It's just for getting data, not writing.

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

+3


source to share


4 answers


Of course, this will improve your performance if you implement it correctly. As mentioned, you just need to be careful with the implementation to avoid security and performance issues. I suggest having a thread pool, so you can manage many threads that make the REST calls that you make at the same time.

ThreadPoolExecutor

will be helpful.

You can use Executors.newFixedThreadPool

/ Executors.newCachedThreadPool

. They both work great.



You can create a task (injection Callable

) to perform your REST activation or call and then use invokeAll(

) ThreadPoolExecutor

(This is where you would like to include the task list).

Hope this helps you.

Sincerely.

+4


source


It is possible. Just be careful how many times you call it because you will exceed your server session limit depending on your server setting. Also if you call this with a lot of threads and fast, you will probably end up with something like a denial of service attack.



+1


source


Yes, you can do it. Even writing data shouldn't be a problem. The only risk is that you might overload your data (connections) much faster, or that your server might not allow it. Other risks are inherent in poor multithreading performance.

+1


source


As others have pointed out, yes it is doable, but you should be aware of the possible load that you would put on the server.

One solution I've used for this kind of thing in the past is a work queue of one kind or another. For example:

ExecutorService es = Executors.newFixedThreadPool(MAX_THREADS);

for (final Thing thing : things) {
    es.submit(new Runnable {
        @Override
        void run() {
            doSomething(thing);
        }
    });
}

es.shutdown();

      

What does it do:

See also: Thread pools

+1


source







All Articles