Вообще хуки они -
They should, in particular, be written to be thread-safe and to avoid deadlocks insofar as possible. They should also not rely blindly upon services that may have registered their own shutdown hooks and therefore may themselves in the process of shutting down. Attempts to use other thread-based services such as the AWT event-dispatch thread, for example, may lead to deadlocks.
...
6 public class TestServerExit { 7 8 9 10 public TestServerExit() { 11 Runnable shutdown = new Runnable() { 12 @Override 13 public void run() { 14 synchronized (TestServerExit.this) { 15 System.out.println("pook!"); 16 } 17 } 18 }; 19 Runtime.getRuntime().addShutdownHook(new Thread(shutdown, "hook")); 20 } 21 22 23 public void stop() { 24 synchronized (this) { 25 System.exit(0); 26 } 27 } 28 29 30 public static void main(String[] args) { 31 TestServerExit server = new TestServerExit(); 32 server.stop(); 33 } 34 }
Комментариев нет:
Отправить комментарий