class ApplicationContext{
private final NetworkObject networkObject = new networkObject();
public ApplicationContext(){
networkObject.setHost("host");
networkObject.setParams("param");
}
public searchObjects(ObjectType objType){
networkObject.doSearch(buildQuery(objType));
}
}
class NetworkObject{
private final SearchObject searchObject = new SearchObject();
public doSearch(SearchQuery searchQuery){
searchObject.search(searchQuery); //threadsafe, takes 15(s) to return
}
}
Consider a webserver running a web application which creates only one ApplicationContext instance (singleton) and uses the same applicationInstance to call searchObjects e.g.
ApplicationContext appInstance =
ApplicationContextFactory.Instance(); //singleton
Every new request to a webpage say ‘search.jsp’ makes a call
appInstance.searchObjects(objectType);
I am making 1000 requests to ‘search.jsp’ page. All the threads are using the same ApplicationContext instance, and searchObject.search() method takes 15 seconds to return. My Question is Do all other threads wait for their turn (15 sec) to execute when one is already executing the searchObject.search() function or All threads will execute searchObject.search() concurrently, Why??
I hope I have made my question very clear??
Update:
Thanks all for clarifying my doubt. Here is my second Question, what difference in performance should be observe when I do:
public synchronized doSearch(SearchQuery searchQuery){
searchObject.search(searchQuery); //threadsafe, takes 15(s) to return
}
OR
public doSearch(SearchQuery searchQuery){
searchObject.search(searchQuery); //threadsafe, takes 15(s) to return
}
I believe using the function ‘doSearch’ without synchronized keyword should be giving more performance. But, when I tested it today, the results came out the other way. The performance was similar or sometimes better when I use synchronized keyword.
Can anyone explain the behavior. How should I debug such cases.
Regards,
Perry
Well you haven’t specified any synchronization in the code, so without any other evidence I’d suspect that all the threads will run concurrently. If
SearchObject.searchcontains some synchronization though, that would obvious limit the concurrency.Mind you, your JSP container is probably using a thread pool to service the 1000 requests, rather than creating 1000 threads.
EDIT: As for why it may be faster with
synchronized: sometimes concurrency isn’t actually helpful to throughput. Things like context switching, disk bottlenecks, cache misses etc have that effect. It’s usually not a good idea to have more running threads than cores.For a real-life example, suppose you have a thousand shoppers who all want to buy things from a fairly small shop. How would you go about it? Put all 1000 in the shop at the same time, or keep it down to a fairly small number in the shop at any one time, and a queue outside?