Question

I have a Java ThreadFactory implementation spawning runnable thread subclass objects in my Android application. This application requires that all spawned threads are addressable before a certain event fires, and that upon the firing of said event the spawned threads can be made eligible for garbage collection (e.g. reference count of 0). I was thinking that to satisfy the first requirement I would simply maintain an ArrayList of my thread objects, and that works fine. The trouble comes with the second requirement, and has led me to a series of questions regarding Java reference counting:

Question 1: Does simply storing references to the spawned threads in an ArrayList or other container increment the reference count of each thread object? What would the reference count of each thread object be if I never stored them, but rather allowed my factory to create them and let them run without any defined handle as below?

mvThreadSource.newThread(new BackgroundThread(...)).run();

Question 2: Does the below code sample do anything to the actual thread object pointed to by hTempThread other than increment and immediately decrement its reference count?

BackgroundThread hTempThread;
for(int i=0;i<mvThreadsVector.size();i++){
    hTempThread = mvThreadsVector.get(i); //probably increments ref count of thread
    hTempThread = null; //probably just decrements the ref count of thread back to 
                        //previous value
}

Question 3: Assuming the answer to question 2 is 'no', what would be an effective way to store threads spawned by a ThreadFactory implementation such that their reference counts can be reduced to 0 on demand? What would be the proper syntax involved in removing these references? Would the below code sample effectively reduce all involved objects' (mvThreadsVector, tempThreads, each thread object tracked by mvThreadsVector) reference counts to 0? What exactly does clear() do to the reference counts of objects stored in an arraylist, and what does setting an array reference to null do to the reference counts of elements stored inside (if anything)?

Object[] tempThreads = mvThreadsVector.toArray();
mvThreadsVector.clear(); //possible this line is all I need...
mvThreadsVector = null;
for(int i=0;i<tempThreads.length;i++){
    tempThreads[i] = null;
}
tempThreads = null;

Any help with any/all of the above questions would be very much appreciated!

Was it helpful?

Solution

  1. If you have your thread running, until it finishes execution it will be kept in memory (or swapped) but not GC. Even if the Thread has no code of yours referencing it but is executing code, it has at least a reference internally.

  2. You cannot even guarantee that it will increment and decrement the reference count. The compiler may optimize it by realizing that the first assignment has no effect (since it is immediately overwritten) and remove it.

  3. The reference count will be reduced but remember that you may have loops in the reference chain, where each element will have 1 reference count but the whole loop is not accessible and a correct GC should realize that and remove it.

I think the main issue is that you shouldn't count on the reference count of threads going down to 0; as long as they are running you should assume they have references so you must also have a way of making sure the thread stops its execution.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top