Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Another Mock Exam Question

 
JiaPei Jen
Ranch Hand
Posts: 1309
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which is the better approach for enabling thread safe servlets - single thread model or synchronization?
a. Depends on the level of user access.
b. Both are equally good.
c. Depends on the number of hits on the site.
Which one is the correct answer?
 
Jim Bertorelli
Ranch Hand
Posts: 136
 
Chintan Rajyaguru
Ranch Hand
Posts: 341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think answer is C, Depends on the number of hits on the site.
IF you have verfy high number of hits and you implement single thread model, the container will queue all the request to a single instance or create multiple instances. This will be very slow.
If you do not implement SingleThread model, you can have multiple threads running and you can synchronize access to shared resources.
A. I do not understand this option
B. Both are not equally good. As I explained above implementing SingleThreadModel makes things slower and synchronizing may create bottleneck.
C. I think this is the answer.
I would love to see more comments.
Chintan
 
g madhava
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which is the better approach for enabling thread safe servlets - single thread model or synchronization?
Depends on the level of user access.
Both are equally good.
Depends on the number of hits on the site.

User access has nothing to do with thread safe servlets and also with the single thread model.User access depends on authentication and authorization.And both the tasks are independent of thread synchronization with in the context given above.
Single Thread model could be used where in the number of hits are less.
while synchronized code block if used correctly could be used for a site which has high number of hits.
So the answer is (C)
 
Jim Bertorelli
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chintan and Madhava, although the answer is C but your explanations are totally opposite to what the posts I refered above, convey.
If a site has a lot of users, you should use SingleThreadModel and not make your methods synchronized. You might want to read those posts again.
 
Jim Bertorelli
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually this question doesn't make sense. It depends on how the servlet is coded or what the servlet can or cannot do. For example, if a servlet uses class variables for sharing, you cannot use STM but if it only uses instance variable, STM will be more efficient than synch. methods. In any case, nobody thinks of using a less efficient approach even if the number of users is less!!!
Where did you get this question???
[ January 11, 2002: Message edited by: Jim Bertorelli ]
 
g madhava
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jim,
Contrary to what was being discussed in that particular thread,IBM says not to use SingleThreadModel.
Take a look at this document:
http://www-4.ibm.com/software/webservers/appserv/ws_bestpractices.pdf

NOTE: The main assumption in using the Java technology for server side applications is that the time taken for the network connections between the client and the server is more than the time taken for processing at the server.
thanks,
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3711
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What the document says is indeed correct. However, it does not compare SingleThreadModel with synchronized doXXX methods approach, which is what the question (and the referred thread) is talking about.
Of course, both the styles should be avoided due to performance issues and it is almost always better to code the servlet in a thread safe manner rather than resorting to these "brute force" technique.
HTH,
Paul.
 
jawwad ahmed
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think the correct answer is c.
if there is a choice d) in which if it would be given that sysnchronization is best.It would be close because if u have synchronized properly where needed because after that u arn't woory about the no of user because in SINGLETHREAD MODEL (may server has not done good implementation and has done synchronized the doxxx method.)
 
jawwad ahmed
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think the correct answer is c.
if there is a choice d) in which if it would be given that sysnchronization is best.It would be close because if u have synchronized properly where needed because after that u arn't woory about the no of user because in SINGLETHREAD MODEL (may server has not done good implementation and has done synchronized the doxxx method.)
 
g madhava
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Paul,
Come on,you got to be kidding.What is the other way in servlets,you would be handling the Synchronization issue without using the
SingleThreadModel interface ??
thanks,
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3711
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
synchronizing the doXXX methods?
 
Ashik Uzzaman
Ranch Hand
Posts: 2373
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would also go for C. But today's Jason Hunters would ofcourse prefer not to use SingleThreadModel in a performance-critical solution and go for synchronizing specific methods, bolcks themselves....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic