The GC probably doesn't bother running at all, since that code is ridiculously simple. Imagine if the GC ran every time an object were eligible for GC? It would run constantly.
Don't try to rely on things that aren't guaranteed.
What are attempting to do with your object counting? Perhaps there's a more elegant solution.
Edit:
If you are interested in keeping track of Threads
and not just normal objects, you're probably more interested in knowing whether a Thread
is running or not. You can track that without messing around with GC.
What's the actual use case you're trying to do?
Here's an example with threads (if I understood what you're trying to do):
Thread[] myThreads = new Thread[10];
public Thread getNewThread(Runnable r) {
for(int i = 0;i < myThreads.length;i++) {
if(myThreads[i] == null || !myThreads[i].isAlive()) {
// Found an unused index
Thread t = new Thread(r);
myThreads[i] = t;
return t;
}
}
return null; // Return null, because there aren't places available
}