An interviewer asked me that
How can you implement a class Foo, where you will be able to count instances of that class. There are more thread
You could wrap the Thread's Runnable
inside another Runnable
that would decrement the counter:
Thread createThread(final Runnable r) {
return new Thread(new Runnable() {
@Override public void run() {
try {
r.run();
} finally {
Foo.decrementCounter();
}
}
});
}
The problem with this is if the Runnable r
creates multiple instances of Foo. You'd have to somehow track how many instances the thread created. You could do so using a ThreadLocal
, and then call decrementCounter()
, in the finally
block, the appropriate number of times. See below for a complete, working example.
If you can avoid it, you should not rely on the behavior of the GC as it is quite impredictable! If you insist into dealing with the Garbage Collector, then you should use reference queues -- and to use it properly, you should study the concept of object reachability: http://docs.oracle.com/javase/7/docs/api/index.html?java/lang/ref/package-summary.html
As a final note, if I were interviewing you, I'd try to make you realize that the code you propose does not perfectly satisfy the requirements: you'd have to make the class final
, or the method incrementCount()
final
or private
. Or, easier, you could increment the count in an instance initializer block: no need to think about methods being overriden in subclasses or newly added constructors not incrementing the count.
A complete example:
public class Foo {
private static final AtomicInteger liveInstances = new AtomicInteger(0);
private static final ThreadLocal threadLocalLiveInstances = new ThreadLocal() {
@Override protected Integer initialValue() { return 0; }
}
// instance initializer (so you won't have problems with multiple constructors or virtual methods called from them):
{
liveInstances.incrementAndGet();
threadLocalLiveInstances.set(threadLocalLiveInstances.get() + 1);
}
public static int getTotalLiveInstances() {
return liveInstances.get();
}
public static int getThreadLocalLiveInstances() {
return threadLocalLiveInstances.get();
}
public static void decrementInstanceCount() {
threadLocalLiveInstances.set(threadLocalLiveInstances.get() - 1);
liveInstaces.decrementAndGet();
}
// ... rest of the code of the class ...
}
class FooCountingThreadFactory implements ThreadFactory {
public Thread newThread(final Runnable r) {
return new Thread(new Runnable() {
@Override public void run() {
try {
r.run();
} finally {
while (Foo.getThreadLocalLiveInstances() > 0) {
Foo.decrementInstanceCount();
}
}
}
});
}
}
This way, you can feed this ThreadFactory to a thread pool, for example, or you can use it yourself when you want to build a thread: (new FooCountingThreadFactory()).newThread(job);
Anyways, there's still a problem with this approach: if a thread creates instances of Foo
and stores them on global scope (read: static
fields), then these instances will still be alive after the thread has died, and the counter will all the same be decremented to 0.