multithreading - Java Threads and Shutdown Hook -


i've run interesting issue. seems if, in java, thread calls system.exit() cannot joined via thread.join().

this causing me issues want use shutdown hook clean after application, such as:

runtime.getruntime.addshutdownhook(new thread() {     public void run() {         reader.join();         writer.join();         in.close();         out.close();     } }); 

the idea ensures threads have finished respective resources, before closing resources. problem there 3 situations shutdown hook can invoked. are:

  1. user hits [ctrl] + [c].
  2. reader thread completes, , calls system.exit().
  3. writer thread completes, , calls system.exit().

the first case, user hits [ctrl] + [c] works fine. in either of other 2 cases, shutdown hook blocks forever. knock-on effect of fact 1 thread.join(), called against thread having called system.exit(), blocks forever.

thus, have 2 questions. firstly, know use thread.join(long millis) instead won't block indefinitely, can think of more elegant solution? secondly, while 1 can call thread.join() against same thread twice , on second occasion return immediately, know of reason why calling thread.join() against thread has called system.exit() blocks indefinitely , doesn't return immediately?

system.exit if successful not return via throwing exception, thread never complete. wasn't issue before shutdown hooks.

workaround usual standard locks (or hack new thread(new runnable() { public void run() { system.exit(0); }}).start();).


Comments

Popular posts from this blog

How to mention the localhost in android -

php - Calling a template part from a post -