As far as I know System.exit triggers shutdown hook(if any hook registered) so while shutdown hook thread is waiting for any other thread to join if other thread calls System.exit() I think a deadlock occurs so is there any solution to register a shutdown hook for certain exit codes such as -1? By this way I can prevent a shutdown hook not to work for some specials exit situations. Thanks in advance.
The shutdown hook mechanism doesn't have any way to run shutdown hooks just for particular exit codes. You could register a shutdown hook which checks the exit code, as discussed here, and decides what to do based on that.