• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Single thread model:mock exam question?

 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Source : http://www.podar.net/cgi-bin/scwcd/answers.pl?question=33&correct=4&user=1

1. public class TestServlet extends HttpServlet implements SingleThreadModel {

2. private static int num = 5;

3. public void doGet(HttpServletRequest req, HttpServletResponse resp) {

4. // do nothing;

5. }

6. }


Is num variable thread safe?

Explanation: Explanation though the class implements SingleThreadModel but since the variable declared is static ,there is still the risk that num is modified by multiple pool instances or multiple named instances simultaneously

I dont understand ? num should be thread safe, as there will be only one thread of this servlet at a time.
[ August 08, 2008: Message edited by: Sunny Jain ]
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
as there will be only one thread of this servlet at a time.

No. There will only be one thread using this servlet instance. But as the answer says, there can be multiple instances, and they all share the same static variables.
 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks..! I got it now..!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic