I created a hook that excute some operation when the form is closed:
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
public void run() {
System.out.println("In shutdown hook");
String dataCrittare = "lgt%";
Date now=new Date();
SimpleDateFormat dateformat = new SimpleDateFormat ("yyyyMMdd-HHmm");
writer.println(dateformat.format(now));
writer.flush();
System.out.println("arrived?");
dispose();
System.exit(1);
}
}, "Shutdown-thread"));
It seems that the program exit, but remains the form blocked on the screen, and i have to close it brutally. Anyone know why the form do not disappear? Thanks
The shutdown hooks get invoked when
System.exit()
is called. You shouldn't callSystem.exit()
inside your shutdown hook since the JVM is already on its way to termination. InsideaddShutdownHook()
, the call toSystem.exit()
deadlocks atShutdown.exit()
(which is called fromSystem.exit()
). Even the comment there says:Your shutdown hook does not get called on form closing! (Except you have some
JFrame.setDefaultCloseOperation(EXIT_ON_CLOSE)
or so.)