img
. .
Chapter 8. TSD
Thread-Specific Data
Java TSD
APIs Used in this Chapter
The Class java.lang.ThreadLocal
In which explanations of thread-specific data, their use, and some implementation details are
provided.
Thread-Specific Data
Sometimes it is useful to have data that is globally accessible to any function, yet still unique to
the thread. Two threads that are printing out data, one in French and the other in Danish, would
find it most convenient to have a private global variable, which they could set to the desired
language. Any function at any depth could then access this variable without the hassle of passing
it at every call.
TSD provides this kind of global data by means of a set of function calls. The techniques used by
POSIX and Java provide the same functionality with one major distinction.
In POSIX, TSD is implemented by creating an array of key offsets to value cells, attached to each
thread structure (Figure 8-1). To use it, you first create a new key, which is then added to the TSD
arrays for all threads.[1] Keys are just variables of type pthread_key_t (which are opaque data
types, most commonly integers), and key creation (initialization is a more descriptive term)
consists of setting the value of the key to the next location. Once the key has been created, you can
access or change the value associated with the key via calls to pthread_getspecific() and
pthread_setspecific().
[1]
Adding the two element to the array need not be done at creation time. It can be more effective
to add the element first at first access time for each threat.
Figure . Thread-Specific Data in POSIX
Search WWH :
Custom Search
Previous Page
Multithreaded Programming with JAVA - Topic Index
Next Page
Multithreaded Programming with JAVA - Bookmarks
Home