Swiftorial Logo
Home
Swift Lessons
AI Tools
Learn More
Career
Resources

Java FAQ: Top Questions

45. What are shutdown hooks in Java?

Shutdown hooks are threads registered with the JVM to execute cleanup tasks when the JVM shuts down, either normally or abruptly (e.g., via Ctrl+C).

  • Registration: Added via Runtime.getRuntime().addShutdownHook .
  • Execution: Runs in parallel during JVM shutdown, order not guaranteed.
  • Constraints: Should be lightweight; avoid long-running tasks.
  • Use Case: Closing resources, logging, or saving state.