Freitag, 13. März 2009

CS4282/CS6282 Internet and Distributed Systems Programming

CS4282/CS6282 Internet and Distributed Systems Programming

Doug Lea : Concurrent Programming in Java

notes on p.105

Using ThreadLocal

1. ThreadLocal allows thread-specific variables to be added in an Ad-Hoc fashion to just about any code.

2. The ThreadLocal class internally maintains a table associating data ( ie, Object references ) with Thread instances. ThreadLocal supports set() and get() methods to access data held by the current Thread.

3. The java.lang.InheritableThreadLocal class extends ThreadLocal to automatically propogate per-thread variables to any threads that are in turn created by the current thread.

4. Most designs employing ThreadLocal may be seen as extensions of the Singleton pattern.

5. Rathe than constructing ONE instance of a resource per program, most applications of ThreadLocals construct ONE instance per thread.

6. ThreadLocal variables are normally declared as static, and usually have package-scoped visibility so they may be accessed by any of a set of methods running in a given thread.



Keine Kommentare: