• Post Reply Bookmark Topic Watch Topic
  • New Topic

Servlets and Threads  RSS feed

 
Simon Harvey
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,
Is the only time I need to synchronise a method in a servlet, when that method accesses a global variable at the top of the servlet.
For example this Wouldnt need sync'd

I'm trying to figure out why most methods dont need to be synchronised. The only thing I can think of is if its only when they access global variables that things might go wrong.
Is it the case that most methods are ok, because when a context switch happens, the internal variables for the method are stored and reiniitalised for when the other thread comes back online
Thanks to anyone who can help
Simon
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi simon
yes. u r right in ur predictions. the methods using a local vars need not be synchronized. its only when we have a instance variables in the Servlet class which are used by methods as that variable will be shared for each thread spawned for the servlet.
tho, i have not used much of such instance vars in a servlet to avoid those synchronization issues BUT sometimes it might be "must" to have those and cope with synchronized issues.
regards
maulin
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!