2

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.

AD.
  • 389
  • 1
  • 5
  • 20
  • 1
    Im surprised the executor is making it sequential. Cannot help without any code snippets. Also take a look at Akka actors. – smk Feb 08 '13 at 13:24
  • it's possible the webservice client library you are using is making the calls synchronized. again, impossible to tell much w/out more details and code. – jtahlborn Feb 08 '13 at 13:28
  • i have edited my question including the code inside my web method – AD. Feb 08 '13 at 14:10
  • 2
    What is the value of `size` in `ExecutorService executor = Executors.newFixedThreadPool(size);` – Danish Feb 08 '13 at 14:11
  • 1
    IMHO there can be 2 reasons: 1) because of the load there are so many threads waiting that they will "seem to be serialized", 2) there is a common resource, e.g. in `data` where the 2 threads synchronize to each other. – gaborsch Feb 08 '13 at 14:16
  • If there are 2 jobs only, maybe you could do one of the jobs *in your current thread*, and start a parallel job for the other job. This way you save the costs of keeping of half of the threads. – gaborsch Feb 08 '13 at 14:23

2 Answers2

0

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 MyThreads 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 Threads 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, e.g. by log).

gaborsch
  • 15,408
  • 6
  • 37
  • 48
0

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.

satya
  • 1
  • 1