• Post Reply Bookmark Topic Watch Topic
  • New Topic

ThreadLocal.initialValue() used for lazy loading ?

 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this article at ibmdW, it has this to say:
The initialValue() method is an optional method that lets you set the initial value of the variable if it has not yet been used in this thread; it allows for a form of lazy initialization.

Help me understand why this is considered lazy loading! Obviously if the ThreadLocal variable has not been instantiated, then it would not contain anything.

Pho
 
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Obviously if the ThreadLocal variable has not been instantiated, then it would not contain anything.

Lazy initialization means something isn't initialized until you try to use it the first time. A ThreadLocal won't call initialValue() until the first time you attempt to use (call get() to access) a ThreadLocal value from a certain Thread. If a Thread never requests a value, no value is ever created. It may be obvious that this is how ThreadLocals should work (an alternative non-lazy approach would be to call initialValue() for each ThreadLocal every time a thread is created. Yuck), but something can simultaniously be obvious and serve as an example of lazy initialization!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!