Java puzzle System.exit and locks
Join the DZone community and get the full member experience.
Join For Free
When you call System.exit() it will stop the execution of the thread at that point and not call any finally blocks.
private static final Object lock = new Object(); public static void main(String... args) { Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { @Override public void run() { System.out.println("Locking"); synchronized (lock) { System.out.println("Locked"); } } })); synchronized (lock) { System.exit(0); } }What does this program print?
Replace System.exit(0) with Thread.currentThread().stop() and run again for comparison.
From http://vanillajava.blogspot.com/2011/12/java-puzzle-systemexit-and-locks.html
Java (programming language)
Lock (computer science)
Opinions expressed by DZone contributors are their own.
Comments