0

I have a singleton class and I need to store some fields that are specific to each thread. I am thinking about either adding those fields as ThreadLocal variables in the singleton or using a synchronized Hashtable with keys being the thread IDs. Any suggestions in terms of which one has a lower overhead? I saw a performance comparison earlier but that isn't exactly what I need.

Community
  • 1
  • 1
JRR
  • 6,014
  • 6
  • 39
  • 59
  • 2
    The fields are thread specific, but they may be accessed by other threads? IOW, why do you need to store them in a singleton class? – didierc Nov 25 '12 at 18:09

1 Answers1

1

ThreadLocal is essentially a HashMap keyed on the thread ID. You'd be better to use a ThreadLocal.

There's also (I believe) further optimisation going on behind the scenes for you with ThreadLocals.

OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213