This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Servlets and the fly likes Thread-safety Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Servlets
Bookmark "Thread-safety" Watch "Thread-safety" New topic


John Daniel

Joined: Mar 27, 2005
Posts: 20
I'm a Java newbie writing a small web application. I've been reading K&B's HF Servlet and JSP book at night in my attempt to catch up on the technology. Anyway in chapter 5, Kathy makes it very clear that servlet instance variables are not thread-safe. I understand that. But what I'm not clear on is what about instance variables of new'ed POJ classes? If my servlet instantiates a class containing instance variables, are those instance variables thread-safe? jd
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63198

Yes, unless you do something like store a reference to them in a servlet instance variable.

When you instantiate an object and store it in a thread-safe reference, such as a method-local variable, other threads have no way to "get at" the instance. Ergo, thread-safe.
[ October 19, 2006: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
John Daniel

Joined: Mar 27, 2005
Posts: 20
Thank you, that's what I wanted to know. jd
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: Thread-safety
It's not a secret anymore!