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