tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Christopher Schultz <>
Subject Re: Is ThreadLocal safe to use in servlets
Date Sun, 24 Jun 2007 20:11:32 GMT
Hash: SHA1


lightbulb432 wrote:
> I have another question about using ThreadLocal as an instance variable in a
> threadsafe Singleton. If the Singleton has 5 instance methods, each of which
> requires use of the value stored in the ThreadLocal, I see one option:
> - Start each method with a call to a private method that returns the value
> in the ThreadLocal


> One thing I thought of (though it's
> not possible, but maybe you can suggest other alternatives) is to somehow
> have an instance variable that stores the value of that call, that each
> method could use directly?

You generally don't want a singleton that has instance members,
otherwise you might end up with threading issues. Use the ThreadLocal as
your repository of information, since you're already doing that.

I recommend a method that fetches the variable from the ThreadLocal.
That way, you can add logic that checks for null and throws an exception
if it's missing... since I assume that the presence of the ThreadLocal
data is essential. This way, your error checking won't clutter each of
the methods that will actually be /using/ that data.

- -chris
Version: GnuPG v1.4.7 (MingW32)
Comment: Using GnuPG with Mozilla -


To start a new topic, e-mail:
To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message