-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace ThreadLocal with a hashtable to support reliable cleanup (#588)
Co-authored-by: Scott M Stark <[email protected]>
- Loading branch information
1 parent
305702f
commit 4d182c5
Showing
3 changed files
with
69 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
core/spi/src/main/java/org/jboss/arquillian/core/spi/ArquillianThreadLocal.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package org.jboss.arquillian.core.spi; | ||
|
||
import java.util.Hashtable; | ||
|
||
/** | ||
* Mapping for ThreadId to a value. Same as "ThreadLocal", but with simpler cleanup. | ||
* | ||
*/ | ||
public class ArquillianThreadLocal<T> { | ||
private Hashtable<Long, T> table = new Hashtable<Long, T>(); | ||
|
||
protected T initialValue() { | ||
return null; | ||
} | ||
|
||
/** | ||
* 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 {@link #initialValue} method. | ||
* | ||
* @return the current thread's value of this thread-local | ||
*/ | ||
public T get() { | ||
Thread t = Thread.currentThread(); | ||
long threadId = t.getId(); | ||
|
||
if (table.containsKey(threadId)) { | ||
return table.get(threadId); | ||
} | ||
else { | ||
T value = initialValue(); | ||
table.put(threadId, value); | ||
return value; | ||
} | ||
} | ||
|
||
/** | ||
* Removes the current thread's value for this thread-local | ||
* variable. | ||
* | ||
*/ | ||
public void remove() { | ||
Thread t = Thread.currentThread(); | ||
long threadId = t.getId(); | ||
|
||
if (table.containsKey(threadId)) { | ||
table.remove(threadId); | ||
} | ||
} | ||
|
||
/** | ||
* Clears the cache | ||
*/ | ||
public void clear() { | ||
table.clear(); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters