JVM shutdown
The JVM can be shut down gracefully or forcibly shut down, and there are several triggering methods for normal shutdown:
- When the last normal (non-daemon, what is going to be a daemon) thread ends
- When System.exit is called, or when it is closed by another platform-specific method (such as sending a SIGINT signal or typing ctrl-c)
- Shut down the JVM by other specific platform methods, call Runtime.halt or kill the JVM process in the operating system (for example, send Sigkill) to forcibly shut down the JVM.
Close Hooks
In normal shutdown, the JVM first invokes all registered closed hooks, which are the threads registered through Runtime.addshutdownhook but not yet started. The JVM does not guarantee the order in which hooks are called off. When the application thread is closed, it is executed concurrently.
When all closure hooks are executed at the end, if Runfinalizersonexit is true, then the JVM runs the finalizer and then stops. The JVM does not stop or break any application threads that are still running when they are shut down.
When the JVM eventually ends, these threads will be forcibly ended. If you close the hook or the finalizer does not complete, the normal shutdown process hangs and the JVM must be forcibly shut down. When forcibly closed, only the JVM is shut down. Instead of running the close hook.
Closing hooks should be thread-safe, and they must use a synchronization mechanism when accessing shared data and avoid deadlocks.
And closing the hook should not be an assumption of the state of the application (e.g. whether other services have been shut down, or whether all normal threads have completed) or the reason for the shutdown of the JVM.
Finally, you should exit the hooks as soon as they are closed, because they will delay the end time of the JVM.
Apps that close hooks: implement cleanup of services or applications, such as deleting temporary files, and so on. Here's a log service for chestnuts.
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 the closure hook is executed concurrently, it should not rely on other services that close the hook. The way to do this is to use the same closed hook for all services, which means that all operations are placed in the same addshutdownhook.
Daemon Threads
The daemon thread is actually a worker thread. For example, all threads created at the start of the JVM, except the main thread, are daemon threads (such as the garbage collector). By default, all threads created by the main thread are normal threads. Similarly, threads created by the daemon thread are all daemon threads, and there are inheritance relationships between threads
The difference between a daemon thread and a normal thread is only the action that occurs when a thread exits:
When a thread exits, the JVM checks for other running threads and, if it is a daemon thread, the JVM exits normally. When the JVM is stopped, all remaining daemon threads are discarded. The finally block is not executed and the volume stack is not returned. Just exit directly.
Terminator
For some resources, such as file handles or socket handles, they must be explicitly returned to the operating system when they are no longer needed, and the garbage collector handles specially the objects that define the Finalize method. Their finalize method is called when they are recycled. A complex finalizer can incur huge overhead, so you should avoid using finalizers.
Reference resources: Java Concurrency programming combat
Task cancellation for Java concurrent Programming (ix)