Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!.POSTED!not-for-mail From: "Mike Schilling" Newsgroups: comp.lang.java.programmer Subject: Re: Proposed new Java feature Date: Sun, 27 May 2012 11:00:41 -0700 Organization: A noiseless patient Spider Lines: 44 Message-ID: References: Injection-Date: Sun, 27 May 2012 18:00:34 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="P5j3IZ/M0ehOxHRbp7DrKw"; logging-data="1656"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18J0rNaq6XXk+vShZuTexSlldinGISmnl8=" X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.6157 X-RFC2646: Format=Flowed; Response X-Newsreader: Microsoft Outlook Express 6.00.2900.5931 Cancel-Lock: sha1:CWH78fkWdw4tQPXGxsBeQptH0lM= X-Priority: 3 X-MSMail-Priority: Normal Xref: csiph.com comp.lang.java.programmer:14833 "markspace" <-@.> wrote in message news:jptkmp$vbg$1@dont-email.me... > On 5/26/2012 4:11 PM, Mike Schilling wrote: > >> Proposed feature: a static method on Thread that clears all ThreadLocals >> for >> the current thread. >> > > > I can see your points. However, I don't have any real experience with > ThreadLocal, and when a neophyte agrees with your argument, that's a red > flag. > > Here's a blog where someone seems to have the same issue as you. > > > > At the end of the comments, there's a suggestion to use > ThreadLocal::remove(), with the implication that it allows the thread > local variable to be garbage collection. Is there a reason that doesn't > work for you? That acts on an individual ThreadLocal (and works quite well), but it doens't allow removing all ThreadLocals that might have been accumlated. > > My other thought is that "for the current thread" could be improved with > "for a given thread." So, inside an Executor, I can just call > > Thread t = ... > // .. use the thread .. > Thread.removeLocals( t ); > // now add the thread back into the pool... > > And this seems better because I don't have to rely on the users of a > thread remembering to do it themselves. External control seems better > here. > Same comment. What I'm asking for is Thread.removeLocals(), which doesn't currently exist.