Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Thread safe servlets  RSS feed

 
Naveen Madarapu
Ranch Hand
Posts: 64
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can any one know about how to write thread safe servlets with out using the synchronized keyword and implementing SingleThreadModel any idea?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66182
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. Follow the general rules for thread safety.

For example, use no read/write data elements that could be corrupted when shared across threads. For servlets, that means no instance variables.

Also be cautious of read/write scoped data in the session or application contexts.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's nothing wrong with using synchronized blocks, provided they're not applied to the entire code flow. Be aware that other approaches to writing thread-safe code are available that may be better suited.

Also note that there's nothing wrong with instance variables in servlets, provided that they're not mutable, or that access to them is synchronized.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!