Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #30284

Re: ThreadLocal question

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail
From Eric Sosman <esosman@comcast-dot-net.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: ThreadLocal question
Date Wed, 05 Nov 2014 08:40:42 -0500
Organization A noiseless patient Spider
Lines 48
Message-ID <m3d9cl$ub7$1@dont-email.me> (permalink)
References <9f82988b-54ea-45f8-acec-5850111705d8@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=windows-1252; format=flowed
Content-Transfer-Encoding 7bit
Injection-Date Wed, 5 Nov 2014 13:40:37 +0000 (UTC)
Injection-Info mx02.eternal-september.org; posting-host="bd4e1cba8d8592bec5d5ed9e4537fbfd"; logging-data="31079"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19DyY19OPeMSy3XV5+jvVfW"
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.2.0
In-Reply-To <9f82988b-54ea-45f8-acec-5850111705d8@googlegroups.com>
Cancel-Lock sha1:qQ8xxUy16yw4oEqDY93ixSz0tz8=
Xref csiph.com comp.lang.java.programmer:30284

Show key headers only | View raw


On 11/5/2014 3:05 AM, mike wrote:
> Hi,
>
> When we do the following:
>
>
> private static ThreadLocal myParameter = new InheritableThreadLocal();
>
> Why dont you do it like this:
>
> private static ThreadLocal myParameter = new ThreadLocal ();
>
> Since myParameter is a ThreadLocal anyway right?

     InheritableThreadLocal is a subclass of ThreadLocal, a "special
kind of" ThreadLocal, if you will.  Every InheritableThreadLocal is
a ThreadLocal, but not every ThreadLocal is an InheritableThreadLocal.
(A Schnauzer is a "special kind of" Dog and every Schnauzer is a Dog,
but not every Dog is a Schnauzer.)

     If you want the "special" behavior of InheritableThreadLocal, a
generic ThreadLocal won't do (you cannot enter just any Dog in a
kennel club Schnauzer show).  Your second initialization is entirely
legal, but it won't give you InheritableThreadLocal behavior.

     A third thing you might have written would be

	private static InheritableThreadLocal myParameter
	    = new InheritableThreadLocal();

... and this would work just fine, giving you the special behavior
you wanted.  However, you might prefer to localize the knowledge of
the special behavior to just the place where the initialization happens,
and not burden the rest of the program with it.  That's what the first
version does: It creates an InheritableThreadLocal to get the special
behavior, and then tells the rest of the program "Here's a ThreadLocal
you can use, and never mind about exactly what flavor it is."  One
benefit of this approach is that if someday you invent yet another
special version of ThreadLocal, like SelfDestructingThreadLocal, you
can drop it in just by changing the initialization, and not need to
go changing Inheritable to SelfDestructing throughout the program.
(If you've declared myParameter as a Dog and initialized it with an
instance of a Schnauzer, you can later switch to a Schnoodle much
more easily than if you'd written Schnauzer everywhere.)

-- 
esosman@comcast-dot-net.invalid
"Don't be afraid of work; make work afraid of you." -- TLM

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

ThreadLocal question mike <mikaelpetterson@hotmail.com> - 2014-11-05 00:05 -0800
  Re: ThreadLocal question Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-11-05 08:40 -0500

csiph-web