简体   繁体   中英

Executing multiple threads in java web service

I have a web service which in one of its web methods creates two threads and calls two more web service for inserting data. Now, my original web service is expected to receive calls in range of 1,000 to 10,000 in parallel. So you may understand that for each such call internally my web service is going to create two more threads. I have already tuned my application server for such heavy load. trust me.

I used Executor with fixed thread pool size using future objects in my original web service but i found that the calls made to the other two web services seem to be processed sequentially. I have checked all tutorials/code snippets. My code seems correct.

ExecutorService executor = Executors.newFixedThreadPool(size);
Future<Boolean> futureOne = executor.submit(new MyThread(serviceOne, data));
Future<Boolean> futureTwo = executor.submit(new  MyThread(serviceTwo, data));
bSuccessfulOne = futureOne.get();
bSuccessfulTwo = futureTwo.get();

In view of the first para that i wrote is there any multithreading/job parallel framework/just some common-sense programming that might help process these web service calls in parallel. If any, please suggest.

As I noted in the prevoius comments, there can be 2 major reasons why it is "serialized":

  1. There is a huge load, it may be that too many request arrive parallelly, so your MyThread s are not put into the waiting queue at the same time. If the number of executing threads is small enough, you may see that the jobs are executes one after the other.
  2. There may be a synchronized resource somewhere is your data . Both threads have access to them.

But I don't really understand why you have to run both jobs in a separate Thread. You could do like this:

ExecutorService executor = Executors.newFixedThreadPool(size);
Future<Boolean> futureOne = executor.submit(new MyThread(serviceOne, data));
Runnable jobTwo = new  MyThread(serviceTwo, data);
jobTwo.run();
bSuccessfulOne = futureOne.get();

This way you need half as much Thread s in your ThreadPool , this will surely give you better performance.

If you stick to the original version, I recommend you to measure (I mean prove your assumption, eg by log).

Your sequential execution is due to below lines: bSuccessfulOne = futureOne.get(); bSuccessfulTwo = futureTwo.get();

Please make a list of future add your result there and stop the thread execution. You may use awaitTermination or some other thing. Then get the future object from list and do get operation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM