• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

static variable/thread safe

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If a servlet does NOT implement SingleThreadModel, then the static variable in this servlet is thread safe, otherwise, (if implement SingleThreadModel) the static varialbe is not thread safe.
Is it right or not?
Thank guys.
Eunis
 
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nope, static vars are never thread-safe. All instances of the servlet will be able to access the static var.
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi eunis,
a static variable still can be accessed by different thread by declaring var.ClassName. if you can tell the difference between a static and a non-static, you'll find static variable is the same as a non-static in thread safty senario.
tony
 
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
By default, a servlet normally has only one servlet instance per servlet definition. Even if we assume or guarantee that only one servlet instance is available to service requests, concurrent requests will spawn multiple threads, so static servlet variables are not thread-safe.
A servlet that implements SingleThreadModel is usually pooled by the container so this situation is not thread-safe for static servlet variables either.
-anthony
[ March 22, 2002: Message edited by: Anthony Villanueva ]
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For completeness: static final variables are the exception., of course.
- Peter
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic