• Post Reply Bookmark Topic Watch Topic
  • New Topic

Synchronously invoking web service using threads?

 
Catherine Maloney
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I want to improve the efficiency of my program and I think adopting a multi-threaded approach may help.

My program converts a list of values from one currency to another using a currency converter web service. Currently my code iterates over the list and invokes the service on each value one after another, as follows:



Is it possible/a better idea to use threads for such a scenario, e.g.for each value in our originalValues list, create a thread that invokes the currency converter web service synchronously? If yes, then is there a maximum number of threads allowed to created?

I apologise if these are basic questions, but I am new to using threads. I would also appreciate if you could point me to relevant articles/forums focused on using threads together with web services.

Thanks,
Catherine
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Catherine,

If I get you correctly, you want to improve the performance by calling the web service for currency conversion in multi threaded way.

IMHO, it depends how may requests, the web service provider can accept at a time, may be there is a limit. You can create multiple threads that will use the input data individually that you have to be converted using web service.
It would be unlike, taking one element from the array one by one and calling web service for that.

You may get your all element from the array to be converted to particular currency type, much early that the single thread way.
[ October 02, 2008: Message edited by: Chandra Bhatt ]
 
Satish Chilukuri
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Catherine Maloney:


Is it possible/a better idea to use threads for such a scenario, e.g.for each value in our originalValues list, create a thread that invokes the currency converter web service synchronously? If yes, then is there a maximum number of threads allowed to created?


If it is possible to change the web service, the best solution would be to make it accept an array of currency values instead of a single value.

The no. of threads you can create depends on the memory available for your application. You can specify the memory sizes using -Xms (min heap size) and -Xmx (max heap size) options.
 
Vinod K Singh
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Executors, ExecutorService, FutureTask classes introduced in Java 5 would of your interest in this particular scenario.
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Spawning multiple threads to do mutually exclusive work does not always guarantee a performance improvement. In fact it can deteriorate the same due to thread scheduling overheads.

However, in your case, since you are making a web service call, it looks like your tasks will not be CPU intensive and hence making the call in a different thread may give you performance boost.

Yes, the maximum number of threads are limited but in most of the cases you should not care about that. What you must care about is how many threads you must have for an optimized performance. These two numbers are hugely different.
So, having 1000 threads on a single CPU machine may be technically possible but it will not give you performance improvement.

As Vinod has pointed out, you will be better off using thread pools(executors in JDK 5 and above) rather than creating a new thread everytime.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!