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


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

Re: Proposed new Java feature

From Kevin McMurtrie <mcmurtrie@pixelmemory.us>
Newsgroups comp.lang.java.programmer
Subject Re: Proposed new Java feature
References <jprnu7$uv8$1@dont-email.me>
Date 2012-06-02 12:41 -0700
Message-ID <4fca6c4f$0$35595$742ec2ed@news.sonic.net> (permalink)
Organization Sonic.Net

Show all headers | View raw


In article <jprnu7$uv8$1@dont-email.me>,
 "Mike Schilling" <mscottschilling@hotmail.com> wrote:

> First a few observations:
> 
> 1. ThreadLocals may be, in general, an abomination, but there are situation 
> where they're the least of evils.  And if you're building a framework in 
> which third-party code runs (e.g. a webserver), there's no way to disallow 
> their use.
> 
> 2. ThreadLocals interact badly with ThreadPools, because the ThreadLocals 
> keep their value when the tyhread is put back into the pool.  This can lead 
> to leaks and even potential security issues.
> 
> 3. The current implementation of ThreadLocal is this: each thread contains a 
> map from the ThreadLocal instance to its value for that thread.  (This is 
> slightly less intiutive than giving a ThreadLocal instance a map from Thread 
> to value, but has the advantage that the map, which is only used within one 
> thread, needn't be synchronized.)
> 
> Proposed feature: a static method on Thread that clears all ThreadLocals for 
> the current thread.  By 3 it's trivial to implement.  By 1 and 2 it's 
> needed.  And ThreadPool implementations can simply call it directly before 
> putting the Thread back into the pool.

I find ThreadLocal useful for caching rapidly used utility objects that 
are stateful and expensive to initialize.  Formatters, regexp parsers, 
etc.  Despite that use, I do still hate the way ThreadLocal works.

A built-in ThreadLocalCache would be better.  It would behave like 
ThreadLocal except use a SoftReference for each element.  That would 
save me the trouble of writing my own when the thread count gets to be 
too high for having a few KB idle in each thread.  (It would be nice if 
SoftReference behaved better too)

Forcing the ThreadLocals out of a Thread would be bad.  There's still 
too much crap code floating around that uses ThreadLocal for parameter 
passing.  You'd get too much random behavior that's nearly impossible to 
debug.  It would be no worse to eliminate ThreadLocal support entirely.
-- 
I will not see posts from Google because I must filter them as spam

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


Thread

Proposed new Java feature "Mike Schilling" <mscottschilling@hotmail.com> - 2012-05-26 16:11 -0700
  Re: Proposed new Java feature Robert Klemme <shortcutter@googlemail.com> - 2012-05-27 13:32 +0200
    Re: Proposed new Java feature "Mike Schilling" <mscottschilling@hotmail.com> - 2012-05-27 11:14 -0700
      Re: Proposed new Java feature Robert Klemme <shortcutter@googlemail.com> - 2012-05-28 12:13 +0200
      Re: Proposed new Java feature v_borchert@despammed.com (Volker Borchert) - 2012-06-04 20:16 +0000
  Re: Proposed new Java feature markspace <-@.> - 2012-05-27 09:28 -0700
    Re: Proposed new Java feature "Mike Schilling" <mscottschilling@hotmail.com> - 2012-05-27 11:00 -0700
      Re: Proposed new Java feature Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-05-27 11:46 -0700
        Re: Proposed new Java feature "Mike Schilling" <mscottschilling@hotmail.com> - 2012-05-27 12:04 -0700
          Re: Proposed new Java feature Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-05-27 15:30 -0400
            Re: Proposed new Java feature "Mike Schilling" <mscottschilling@hotmail.com> - 2012-05-27 12:59 -0700
              Re: Proposed new Java feature Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-05-27 17:51 -0400
                Re: Proposed new Java feature markspace <-@.> - 2012-05-27 15:20 -0700
                Re: Proposed new Java feature Robert Klemme <shortcutter@googlemail.com> - 2012-05-28 12:13 +0200
                Re: Proposed new Java feature markspace <-@.> - 2012-05-28 08:28 -0700
                Re: Proposed new Java feature Robert Klemme <shortcutter@googlemail.com> - 2012-05-28 19:29 +0200
                Re: Proposed new Java feature markspace <-@.> - 2012-05-28 12:16 -0700
                Re: Proposed new Java feature Robert Klemme <shortcutter@googlemail.com> - 2012-05-28 22:58 +0200
                Re: Proposed new Java feature "Mike Schilling" <mscottschilling@hotmail.com> - 2012-05-28 21:40 -0700
                Re: Proposed new Java feature Robert Klemme <shortcutter@googlemail.com> - 2012-05-29 22:52 +0200
                Re: Proposed new Java feature "Mike Schilling" <mscottschilling@hotmail.com> - 2012-05-29 18:53 -0700
                Re: Proposed new Java feature Tom Anderson <twic@urchin.earth.li> - 2012-05-27 23:27 +0100
                Re: Proposed new Java feature Robert Klemme <shortcutter@googlemail.com> - 2012-05-28 12:22 +0200
                Re: Proposed new Java feature "Mike Schilling" <mscottschilling@hotmail.com> - 2012-05-27 21:43 -0700
                Re: Proposed new Java feature Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2012-05-29 14:32 +0000
  Re: Proposed new Java feature Tom Anderson <twic@urchin.earth.li> - 2012-05-27 18:43 +0100
  Re: Proposed new Java feature Kevin McMurtrie <mcmurtrie@pixelmemory.us> - 2012-06-02 12:41 -0700
    Re: Proposed new Java feature markspace <-@.> - 2012-06-02 15:55 -0700

csiph-web