Top Description Inners Fields Constructors Methods
org.glassfish.hk2.utilities.general

public Class Hk2ThreadLocal<V>

extends Object
Class Inheritance
Imports
java.lang.ref.ReferenceQueue, .WeakReference, java.util.Map, java.util.concurrent.ConcurrentHashMap

This is a poor mans version of a java.lang.ThreadLocal with the one major upside of a removeAll() method that can be used to remove ALL instances of all thread locals on ALL threads from any other thread.
Authors
jwells, Bryan Atsatt

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
Hk2ThreadLocal.Key

A weakly referenced thread suitable as a map key.

Field Summary

Modifier and TypeField and Description
private final Map<Hk2ThreadLocal.Key, Object>
private static final Object
private final ReferenceQueue<Thread>

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public V

Returns:

the current thread's value of this thread-local
get
()

Returns the value in the current thread's copy of this thread-local variable.

public int

Returns:

The current number of entries across all threads. This is basically the number of threads that currently have data with the Hk2ThreadLocal
getSize
()

Returns the total size of the internal data structure in terms of entries.

protected V

Returns:

the initial value for this thread-local
initialValue
()

Returns the current thread's "initial value" for this thread-local variable.

private static Object

Returns:

The value or sentinel.
maskNull
(Object
The value.
value
)

Replace a null value with a sentinel that can be stored in the map.

private static Hk2ThreadLocal.Key

Returns:

The key.
newLookupKey
()

Create a key for the current thread that will not be enqueued.

private static Hk2ThreadLocal.Key

Returns:

The key.
newStorageKey
(ReferenceQueue<Thread>
The reference queue.
queue
)

Create a key for the current thread that will be enqueued unless cleared.

public void
remove()

Removes the current thread's value for this thread-local variable.

public void
removeAll()

Removes all threads current thread's value for this thread-local variable.

private void
removeStaleEntries()

Removes from the map any key that has been enqueued by the garbage collector.

public void
set(V
the value to be stored in the current thread's copy of this thread-local.
value
)

Sets the current thread's copy of this thread-local variable to the specified value.

private static Object

Returns:

The value or null.
unmaskNull
(Object
The value or sentinel.
value
)

Replace a sentinel with null.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

localsback to summary
private final Map<Hk2ThreadLocal.Key, Object> locals
NULLback to summary
private static final Object NULL
queueback to summary
private final ReferenceQueue<Thread> queue

Constructor Detail

Hk2ThreadLocalback to summary
public Hk2ThreadLocal()

Method Detail

getback to summary
public V get()

Returns the value in the current thread's copy of this thread-local variable. If the variable has no value for the current thread, it is first initialized to the value returned by an invocation of the initialValue method.

Returns:V

the current thread's value of this thread-local

Annotations
@SuppressWarnings:unchecked
getSizeback to summary
public int getSize()

Returns the total size of the internal data structure in terms of entries. This is used for diagnostics purposes only

Returns:int

The current number of entries across all threads. This is basically the number of threads that currently have data with the Hk2ThreadLocal

initialValueback to summary
protected V initialValue()

Returns the current thread's "initial value" for this thread-local variable. This method will be invoked the first time a thread accesses the variable with the get method, unless the thread previously invoked the set method, in which case the initialValue method will not be invoked for the thread. Normally, this method is invoked at most once per thread, but it may be invoked again in case of subsequent invocations of remove followed by get.

This implementation simply returns null; if the programmer desires thread-local variables to have an initial value other than null, ThreadLocal must be subclassed, and this method overridden. Typically, an anonymous inner class will be used.

Returns:V

the initial value for this thread-local

maskNullback to summary
private static Object maskNull(Object value)

Replace a null value with a sentinel that can be stored in the map.

Parameters
value:Object

The value.

Returns:Object

The value or sentinel.

newLookupKeyback to summary
private static Hk2ThreadLocal.Key newLookupKey()

Create a key for the current thread that will not be enqueued. Using this method (or clearing a 'storage' key) ensures that no extra work is required in removeStaleEntries().

Returns:Hk2ThreadLocal.Key

The key.

newStorageKeyback to summary
private static Hk2ThreadLocal.Key newStorageKey(ReferenceQueue<Thread> queue)

Create a key for the current thread that will be enqueued unless cleared. All keys actually stored in the map must be created by this method and must not be explicitly cleared.

Parameters
queue:ReferenceQueue<Thread>

The reference queue.

Returns:Hk2ThreadLocal.Key

The key.

removeback to summary
public void remove()

Removes the current thread's value for this thread-local variable. If this thread-local variable is subsequently read by the current thread, its value will be reinitialized by invoking its initialValue method, unless its value is set by the current thread in the interim. This may result in multiple invocations of the initialValue method in the current thread.

removeAllback to summary
public void removeAll()

Removes all threads current thread's value for this thread-local variable. If this thread-local variable is subsequently read by the current thread, its value will be reinitialized by invoking its initialValue method, unless its value is set by the current thread in the interim. This may result in multiple invocations of the initialValue method in the current thread.

removeStaleEntriesback to summary
private void removeStaleEntries()

Removes from the map any key that has been enqueued by the garbage collector.

setback to summary
public void set(V value)

Sets the current thread's copy of this thread-local variable to the specified value. Most subclasses will have no need to override this method, relying solely on the initialValue method to set the values of thread-locals.

Parameters
value:V

the value to be stored in the current thread's copy of this thread-local.

unmaskNullback to summary
private static Object unmaskNull(Object value)

Replace a sentinel with null.

Parameters
value:Object

The value or sentinel.

Returns:Object

The value or null.

org.glassfish.hk2.utilities.general back to summary

private Class Hk2ThreadLocal.Key

extends WeakReference<Thread>
Class Inheritance

A weakly referenced thread suitable as a map key.

Field Summary

Modifier and TypeField and Description
private final int
private final long

Constructor Summary

AccessConstructor and Description
private
Key(Thread thread, ReferenceQueue<Thread> queue)

Method Summary

Modifier and TypeMethod and Description
public boolean
equals(Object obj)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.

Field Detail

hashback to summary
private final int hash
threadIdback to summary
private final long threadId

Constructor Detail

Keyback to summary
private Key(Thread thread, ReferenceQueue<Thread> queue)

Method Detail

equalsback to summary
public boolean equals(Object obj)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Annotations
@Override
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for this object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Annotations
@Override