Java concurrent programming task cancellation (9)
Disable Jvm
Jvm can be shut down normally or forcibly. There are multiple triggering methods for normal shutdown:
When the last normal (non-daemon, what is the daemon thread) thread ends, the system is called. when you exit, or use other platform-specific methods to close the jvm (for example, sending a SIGINT signal or typing Ctrl-c), call the Runtime. halt or kill JVM processes (such as sending sigkill) in the operating system to forcibly disable jvm. Disable hook
During Normal shutdown, jvm first calls all registered closed hooks. Closing hooks refer to threads registered through Runtime. addShutdownHook but not started yet. The JVM does not guarantee the order in which the hooks are called. When the application thread is closed, it is executed concurrently.
When all closed hooks are executed, if runFinalizersOnExit is true, the jvm runs the Terminator and then stops. The jvm does not stop or interrupt any application thread that is still running when it is closed.
When the jvm ends, these threads are forcibly terminated. If the hook or Terminator is disabled, the process is suspended normally and the jvm must be forcibly disabled. When it is forcibly disabled, it only disables jvm. Instead of running the close hook.
Closing hooks should be thread-safe. They must use the synchronization mechanism when accessing shared data and avoid deadlocks.
In addition, you should not make assumptions about the application state (for example, whether other services have been closed, or whether all normal threads have been executed) or the reason for jvm's shutdown.
Finally, you should exit the hook as soon as possible because they will delay the jvm end time.
Disable hook applications: Clean up services or applications, such as deleting temporary files. The following is a log service example.
public void start() {Runtime.getRuntime().addShutdownHook(new Thread() {@Overridepublic void run() {// TODO Auto-generated method stubtry {logservice.stop();} catch (Exception e) {// TODO: handle exception}}});}
Because disabling a hook is executed concurrently, it should not rely on other services that close the hook. The method to implement this function is to use the same close hook for all services, that is to say, all operations are placed in the same addshutdownhook.
Daemon thread
The daemon thread is actually a secondary thread. For example, all threads created at jvm startup, except the main thread, are daemon threads (such as garbage collector ). By default, all threads created by the main thread are normal threads. Similarly, the threads created by the daemon thread are all daemon threads, and there is an inheritance relationship between threads.
The difference between a daemon thread and a common thread is only the operations that occur when the thread exits:
When a thread exits, the jvm checks other running threads. If it is a daemon thread, the jvm Exits normally. When the jvm stops, all existing daemon threads will be discarded. Does not execute finally blocks or roll back the stack. Exit directly.
Terminator
Some resources, such as file handles or socket handles, must be explicitly handed back to the operating system when they are no longer needed. The Garbage Collector will perform special processing on objects that define the finalize method. When they are recycled, their finalize method is called. Complex Terminators produce huge overhead, so you should avoid using Terminators.
Reference resources: java concurrent programming practices