I want to write a proxy to ensure a particular object is only ever used on a particular thread, so I need to hold a reference to the Thread
object (as the JavaDoc says the id can be reused). Would this reference cause the thread (not the Thread object) to not be fully garbage collected when it terminates?

- 7,187
- 3
- 39
- 79
-
2It would cause the `Thread` not to be garbage-collected, and all objects reachable from it. The *thread* is an operating system entity and isn't subject to garbage-collection at all. Your question doesn't make sense. – user207421 Mar 30 '15 at 09:26
-
I think I am already as clear as I can: there's the instance of the `Thread` class (i.e. the `Thread` object) that my own instance wants to hold and there's the JVM thread (as a scheduling concept). The former exposes methods to operate the latter, but it is unclear whether holding references to the former will prevent the latter to be fully garbage collected (considering ThreadLocals and experiences about thread leaks). – billc.cn Mar 30 '15 at 09:35
-
Not only your question sounds weird, but also the whole design. Why exactly do you think that only one specific thread should be allowed to update (read, write?) a specific object? I mean: is the lifetime of that object and that "manipulation" thread different? – GhostCat Mar 30 '15 at 09:36
-
@EddyG I admit this is an unusual pattern. The external library I use has an object pool, but somewhere in the code base objects are being returned to the pool prematurely resulting in a non-thread-safe object being used on multiple threads. I want to write a proxy to detect such case. The proxy should store a reference to the `currentThread` when the object was taken out of the pool and throw if the calling thread is different from the stored `Thread`. – billc.cn Mar 30 '15 at 09:50
-
I would say that it's a JVM specific topic whose answer is not specified in the Java standard (look at the JVM internals) – superbob Mar 30 '15 at 09:54
-
@JarrodRoberson I failed to see how my question is related to circular reference? (Specifically everything is strong reachable; otherwise I won't worry about memory leaks at all.) I admit superbob's comment pretty much concludes the investigation, but the close is quite misleading. – billc.cn Mar 30 '15 at 21:18
1 Answers
I want to write a proxy to ensure a particular object is only ever used on a particular thread, so I need to hold a reference to the Thread object
You don't. You can use ThreadLocal<Boolean>
which gets set in the particular thread only.
Would this reference cause the thread (not the Thread object) to not be fully garbage collected when it terminates?
Your reference would cause the Thread
to leak, but as @superbob wrote, it's unspecified what happens to the OS thread. I wouldn't care as long as you know it's really a single thread only.
While ThreadLocal
may leak too, a simple ThreadLocal<Boolean>
without subclassing (especially using no initialValue()
) leaks nothing.
The external library I use has an object pool, but somewhere in the code base objects are being returned to the pool prematurely resulting in a non-thread-safe object being used on multiple threads. I want to write a proxy to detect such case. The proxy should store a reference to the currentThread when the object was taken out of the pool and throw if the calling thread is different from the stored Thread.
This sounds pretty different from your question and seems to make my above answer void. I wrote such a thing some long time ago:
import com.google.common.collect.MapMaker;
public static synchronized boolean singleThreaded(Object o) {
final Thread neu = Thread.currentThread();
final Thread old = threadMap.put(o, neu);
return old==null || old==neu;
}
private final static Map<Object, Thread> threadMap =
new MapMaker().weakKeys().weakValues().makeMap();
and used it when debugging for ensuring that someObject
gets used in a single thread via
assert singleThreaded(someObject)
Because of all references being weak, I can hope there's no leak

- 1
- 1

- 44,714
- 32
- 161
- 320