tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Christopher Schultz <ch...@christopherschultz.net>
Subject Re: Is ThreadLocal safe to use in servlets
Date Sun, 24 Jun 2007 20:11:32 GMT
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

lb,

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

[or]

> 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
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGfs/09CaO5/Lv0PARAhhxAJ93d6rQMrZozGJy6tg4Wclda5/+zgCfZBAi
5W7N37Izfp4L3jGYlrlCtZ0=
=zLdP
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Mime
View raw message