• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

ThreadLocal & static

 
Ranch Hand
Posts: 782
Python Chrome Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have a Util class with a static method process which will accept a tree data structure (Composite Pattern). The problem is I want to prevent the possibility of a StackOveflow if the user of this method is stupid enough to introduce loops within the tree.

What I want to do is to keep a list of nodes which I have already visited. So during the navigation down the tree, I will add the visited nodes to this list.

I want to use a ThreadLocal variable to keep the list of visited nodes.
The issue here is that the variable has to be declared static.

I don't understand the semantics of static ThreadLocal variable.
Will different threads that call the process method be using the
same ThreadLocal variable ? Are there any huge design flaws ? Thanks all

 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All threads will be using the same ThreadLocalList object. But the whole point of ThreadLocal is that each thread gets its own value to work with. Therefore, the different threads will not see or corrupt each others' lists.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic