shutdown-hook

How does CTRL-C work with Java program

人盡茶涼 提交于 2019-12-10 12:36:24
问题 When I press ctrl - c in console in what sequence are application threads stopped and shutdown hooks called? 回答1: According to the javadocs, the registered shutdown hooks are called in an unspecified order when the JVM starts shutting down; e.g. in response to a CTRL-C. Application threads are not "stopped" in any well defined way. Indeed, they could continue running up right until the process exits. If you want your threads to be shut down in an orderly fashion, you need to do something in a

What happens if System.exit is called from a shutdown hook?

喜欢而已 提交于 2019-12-09 15:00:14
问题 I have a rather complicated shutdown in Java - there's a lot of clean up work to do. In particular I'm trying to figure out how to error handle from a shutdown hook thread. My code includes this currently: try { return shutdownPromise = doShutdown(); } catch (Throwable exc) { logger.error("An exception was thrown while shutting down the application.", exc); System.exit(1); return null; } When I originally wrote this I essentially thought, an error in shutdown should just go straight to exit .

How does a JVM running multiple threads handle ctrl-c, w/ and w/o shutdown hooks?

筅森魡賤 提交于 2019-12-07 18:24:01
问题 Could not find this answer online. When Ctrl+C is hit: When we don't have any shutdown hook, what happens to the running threads - do they each get hit with an InterruptedException? When we have shutdown hook(s), I know that the shutdown hooks get run in new threads in arbitrary order. But what happens to the existing running threads? Do they still each get hit with an InterruptedException? Thanks! 回答1: The classic book "Java Concurrency in Practice" has a chapter (7.4) on the JVM shutdown,

Why does ShutdownHookThread 'setDaemon true'

耗尽温柔 提交于 2019-12-07 10:29:29
问题 I recently needed to add a shutdown hook to a Scala app I have, and I discovered that Scala provides a helper for this called ShutdownHookThread. In its source I noticed that it sets the new thread to be a daemon thread. def apply(body: => Unit): ShutdownHookThread = { val t = new ShutdownHookThread(hookName()) { override def run() = body } t setDaemon true // <--------- right here runtime addShutdownHook t t } Why is this done? It seems to me you'd probably want the opposite in a shutdown

How to do graceful shutdown/termination of java processes?

落花浮王杯 提交于 2019-12-07 08:34:27
问题 I am running some java apps and I need to shutdown/close all apps gracefully from windows bat script. So my question is: How to invoke shutdown hook by windows bat script and gracefully shutdown java program. Any suggestion is appreciated. Thanks in advance. 回答1: A "graceful" shutdown in Java is generally achieved by letting all non-daemon threads complete their work normally. If you have your app listen for a "shutdown" command on some port, then you could have the script trigger the command

Is there something like finally() in Go just opposite to what init()?

守給你的承諾、 提交于 2019-12-07 02:32:29
问题 Is there something in Go which do just opposite to what init() do inside a package? 回答1: This was discussed before by the Go team, and the conclusion was not to add support for it. Quoting minux: Personally, I prefer the style where program exit is handled exactly same as program crash. I believe no matter how hard you try, your program can still crash under some unforeseen situations; for example, memory shortage can bring any well-behave Go program to a crash, and there is nothing you can

Cygwin CTRL-C (Signal Interrupts) not working properly - JVM Shutdown Hooks not starting

陌路散爱 提交于 2019-12-06 17:18:39
问题 I'm working on a Java application that utilises shutdown hooks in order to clean up on termination/interruption of the program, but I've noticed that Cygwin's implementation of CTRL-C doesn't seem to trigger the shutdown hooks. On the surface it appears to have interrupted the process, relinquishing control back to the command line, however the process' shutdown hooks are not triggered at all so cleanup does not occur. In cmd they get caught, but due to various constraints I need to somehow

How to implement thread pool that will automatically shutdown at end of execution?

安稳与你 提交于 2019-12-06 07:55:25
I'm writing a Java client which could theoretically be used in a different environment: Java main(), in a servlet container, or via dependency injection. The client implements internal connection thread pooling. The problem with this approach is that users of the client that are unaware of the fact that an internal thread pool is implemented will see his or her application "hang" on shutdown. My users need to know to send a shutdown() message to the library. I'm wondering if any other alternative approach could be taken that would, on one hand, allow me to start a thread pool for my

How does a JVM running multiple threads handle ctrl-c, w/ and w/o shutdown hooks?

被刻印的时光 ゝ 提交于 2019-12-06 03:13:23
Could not find this answer online. When Ctrl+C is hit: When we don't have any shutdown hook, what happens to the running threads - do they each get hit with an InterruptedException? When we have shutdown hook(s), I know that the shutdown hooks get run in new threads in arbitrary order. But what happens to the existing running threads? Do they still each get hit with an InterruptedException? Thanks! The classic book "Java Concurrency in Practice" has a chapter (7.4) on the JVM shutdown, you should read that, but here are some relevant quotes: If any application threads (daemon or nondaemon) are

Could not load org.apache.hadoop.util.ShutdownHookManager when shutdown tomcat server

拟墨画扇 提交于 2019-12-05 13:16:21
I have a simple web application run on tomcat. This web application read and write file to HDFS. The issue I am facing is each time when I stop the server by using ./bin/shutdown.sh . I am getting could not load hadoop shutdownHookManager exception. I am sure the hadoop-common (contains ShutDownManager) is in tomcat classpath. Can anyone help me out? Exception I got: Oct 14, 2013 5:57:54 PM org.apache.catalina.loader.WebappClassLoader checkThreadLocalMapForLeaks SEVERE: The web application [] created a ThreadLocal with key of type [com.ebay.kernel.calwrapper.CalTransactionHelper$1] (value [com