I have been struggling for a few hours already on that nonsensical problem, so now I'm seeking for help.
1 liner: my shutdown hook is apparently terminated before it finishes its duty.
The code I'm running looks like this:
class Test {
static {
Runtime.getRuntime().addShutdownHook(new Thread(Test::shutdown));
}
// more methods
private static void shutdown() {
Collection<Foo> coll = some.instance.ofHashMap.values();
System.out.println("collSize=" + coll.size());
for (Foo o : coll) {
System.out.println("Calling bar on foo");
o.bar();
}
System.out.println("End of hook");
}
}
But the output is:
collSize=1
If I run the jvm with the -server option, I might get lucky and the "Calling bar on foo" print but it still doesn't print "End of hook".
Unfortunately, I haven't been able to create a short executable example that shows the problem. I have one but it shows everything's working fine and the collection is iterated as expected. The code above is an extract of a much bigger program i'm working on, but my shut down is hook is doing nothing more than the above (get a non empty collection and iterate).
Any ideas of what might be happening? I am totally lost here...
Notes: - Code is written, compiled and run for/with Java 8u45 - I kill the program with Ctrl+C, as a result, the JVM exists with code 130
Turns out:
Thank you all for the quick replies and help.