• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Single thread model:mock exam question?

 
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks..! I got it now..!
 
reply
    Bookmark Topic Watch Topic
  • New Topic