.....

......

Synchronously invoking web service using threads

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:

code:


Integer[] originalValues = {5.8, 7.3, 12.6};
Integer[] convertedValues = new Integer[originalValues.length];
for(int i = 0; i < originalValues.length; i++)
{
convertedValues[i] = currencyWebServicePort.getConvertedValue( originalValues[i] );
}




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.

Better design

I have 3 instance variables in my class. In a few methods var1 and var2 are used, in some var2 and var3 are used and in some var1 and var2 are used. For synchronization is it better to have lock on "this" or is it good to have three separate locks , one lock for each variable. What makes a good design decision ?


I like the granularity of more locks, one for each variable, or more appropriately one for each 'unit of data' which may mean multiple variables if they are always used together.

It is always best to keep you synchronized blocks as small as possible, and blocking as few threads as possible. Why block all thread's access to all data, when a method only needs 2 variables? Instead just lock those 2 variables so any method trying to use other data can do so.

.....

More.....