java.lang.Runtime.addShutdownHook()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(8.4k)|赞(0)|评价(0)|浏览(170)

本文整理了Java中java.lang.Runtime.addShutdownHook()方法的一些代码示例,展示了Runtime.addShutdownHook()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Runtime.addShutdownHook()方法的具体详情如下:
包路径:java.lang.Runtime
类名称:Runtime
方法名:addShutdownHook

Runtime.addShutdownHook介绍

[英]Registers a VM shutdown hook. A shutdown hook is a Thread that is ready to run, but has not yet been started. All registered shutdown hooks will be executed when the VM terminates normally (typically when the #exit(int) method is called).

Note that on Android, the application lifecycle does not include VM termination, so calling this method will not ensure that your code is run. Instead, you should use the most appropriate lifecycle notification ( Activity.onPause, say).

Shutdown hooks are run concurrently and in an unspecified order. Hooks failing due to an unhandled exception are not a problem, but the stack trace might be printed to the console. Once initiated, the whole shutdown process can only be terminated by calling halt().

If #runFinalizersOnExit(boolean) has been called with a true argument, garbage collection and finalization will take place after all hooks are either finished or have failed. Then the VM terminates.

It is recommended that shutdown hooks do not do any time-consuming activities, in order to not hold up the shutdown process longer than necessary.
[中]注册VM关闭挂钩。关机挂钩是一个准备运行但尚未启动的线程。当VM正常终止时(通常在调用#exit(int)方法时),将执行所有注册的关闭挂钩。
请注意,在Android上,应用程序生命周期不包括VM终止,因此调用此方法将无法确保代码运行。相反,您应该使用最合适的生命周期通知(比如Activity.onPause)。
关机挂钩以未指定的顺序并发运行。由于未处理的异常而导致挂钩失败不是问题,但堆栈跟踪可能会打印到控制台。一旦启动,整个关闭过程只能通过调用halt()终止。
如果使用true参数调用了#runFinalizersOnExit(boolean),则垃圾收集和终结将在所有挂钩完成或失败后进行。然后VM终止。
建议关闭挂钩不要执行任何耗时的活动,以免将关闭过程拖得过长。

代码示例

代码示例来源:origin: google/guava

@VisibleForTesting
 void addShutdownHook(Thread hook) {
  Runtime.getRuntime().addShutdownHook(hook);
 }
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Register a shutdown hook with the JVM runtime, closing this context
 * on JVM shutdown unless it has already been closed at that time.
 * <p>Delegates to {@code doClose()} for the actual closing procedure.
 * @see Runtime#addShutdownHook
 * @see #close()
 * @see #doClose()
 */
@Override
public void registerShutdownHook() {
  if (this.shutdownHook == null) {
    // No shutdown hook registered yet.
    this.shutdownHook = new Thread() {
      @Override
      public void run() {
        synchronized (startupShutdownMonitor) {
          doClose();
        }
      }
    };
    Runtime.getRuntime().addShutdownHook(this.shutdownHook);
  }
}

代码示例来源:origin: apache/incubator-dubbo

/**
 * Register the ShutdownHook
 */
public void register() {
  if (!registered.get() && registered.compareAndSet(false, true)) {
    Runtime.getRuntime().addShutdownHook(getDubboShutdownHook());
  }
}

代码示例来源:origin: apache/incubator-dubbo

/**
 * Register the ShutdownHook
 */
public void register() {
  if (!registered.get() && registered.compareAndSet(false, true)) {
    Runtime.getRuntime().addShutdownHook(getDubboShutdownHook());
  }
}

代码示例来源:origin: stanfordnlp/CoreNLP

/**
 * Close tracks when the JVM shuts down.
 * @return this
 */
public RedwoodConfiguration neatExit(){
 tasks.add(() -> Runtime.getRuntime().addShutdownHook(new Thread(){
  @Override public void run(){ Redwood.stop(); }
 }));
 return this;
}

代码示例来源:origin: libgdx/libgdx

/** When true, <code>Runtime.getRuntime().halt(0);</code> is used when the JVM shuts down. This prevents Swing shutdown hooks
 * from causing a deadlock and keeping the JVM alive indefinitely. Default is true. */
public void setHaltOnShutdown (boolean halt) {
  if (halt) {
    if (shutdownHook != null) return;
    shutdownHook = new Thread() {
      public void run () {
        Runtime.getRuntime().halt(0); // Because fuck you, deadlock causing Swing shutdown hooks.
      }
    };
    Runtime.getRuntime().addShutdownHook(shutdownHook);
  } else if (shutdownHook != null) {
    Runtime.getRuntime().removeShutdownHook(shutdownHook);
    shutdownHook = null;
  }
}

代码示例来源:origin: libgdx/libgdx

/** When true, <code>Runtime.getRuntime().halt(0);</code> is used when the JVM shuts down. This prevents Swing shutdown hooks
 * from causing a deadlock and keeping the JVM alive indefinitely. Default is true. */
public void setHaltOnShutdown (boolean halt) {
  if (halt) {
    if (shutdownHook != null) return;
    shutdownHook = new Thread() {
      public void run () {
        Runtime.getRuntime().halt(0); // Because fuck you, deadlock causing Swing shutdown hooks.
      }
    };
    Runtime.getRuntime().addShutdownHook(shutdownHook);
  } else if (shutdownHook != null) {
    Runtime.getRuntime().removeShutdownHook(shutdownHook);
    shutdownHook = null;
  }
}

代码示例来源:origin: libgdx/libgdx

/** When true, <code>Runtime.getRuntime().halt(0);</code> is used when the JVM shuts down. This prevents Swing shutdown hooks
 * from causing a deadlock and keeping the JVM alive indefinitely. Default is true. */
public void setHaltOnShutdown (boolean halt) {
  if (halt) {
    if (shutdownHook != null) return;
    shutdownHook = new Thread() {
      public void run () {
        Runtime.getRuntime().halt(0); // Because fuck you, deadlock causing Swing shutdown hooks.
      }
    };
    Runtime.getRuntime().addShutdownHook(shutdownHook);
  } else if (shutdownHook != null) {
    Runtime.getRuntime().removeShutdownHook(shutdownHook);
    shutdownHook = null;
  }
}

代码示例来源:origin: libgdx/libgdx

/** When true, <code>Runtime.getRuntime().halt(0);</code> is used when the JVM shuts down. This prevents Swing shutdown hooks
 * from causing a deadlock and keeping the JVM alive indefinitely. Default is true. */
public void setHaltOnShutdown (boolean halt) {
  if (halt) {
    if (shutdownHook != null) return;
    shutdownHook = new Thread() {
      public void run () {
        Runtime.getRuntime().halt(0); // Because fuck you, deadlock causing Swing shutdown hooks.
      }
    };
    Runtime.getRuntime().addShutdownHook(shutdownHook);
  } else if (shutdownHook != null) {
    Runtime.getRuntime().removeShutdownHook(shutdownHook);
    shutdownHook = null;
  }
}

代码示例来源:origin: google/j2objc

@VisibleForTesting
 void addShutdownHook(Thread hook) {
  Runtime.getRuntime().addShutdownHook(hook);
 }
}

代码示例来源:origin: springside/springside4

/**
 * 注册JVM关闭时的钩子程序
 */
public static void addShutdownHook(Runnable runnable) {
  Runtime.getRuntime().addShutdownHook(
      new Thread(runnable, "Thread-ShutDownHook-" + shutdownHookThreadIndex.incrementAndGet()));
}

代码示例来源:origin: eclipse-vertx/vert.x

/**
 * Registers a shutdown hook closing the given vert.x instance when the JVM is terminating.
 * Optionally, an action can be executed after the termination of the {@link Vertx} instance.
 *
 * @param vertx  the vert.x instance, must not be {@code null}
 * @param log    the log, must not be {@code null}
 * @param action the action, may be {@code null}
 */
protected static void addShutdownHook(Vertx vertx, Logger log, Runnable action) {
 Runtime.getRuntime().addShutdownHook(new Thread(getTerminationRunnable(vertx, log, action)));
}

代码示例来源:origin: prestodb/presto

@VisibleForTesting
 void addShutdownHook(Thread hook) {
  Runtime.getRuntime().addShutdownHook(hook);
 }
}

代码示例来源:origin: Netflix/zuul

private ServerGroup(String name, int acceptorThreads, int workerThreads, EventLoopGroupMetrics eventLoopGroupMetrics) {
  this.name = name;
  this.acceptorThreads = acceptorThreads;
  this.workerThreads = workerThreads;
  this.eventLoopGroupMetrics = eventLoopGroupMetrics;
  Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
    public void uncaughtException(final Thread t, final Throwable e) {
      LOG.error("Uncaught throwable", e);
    }
  });
  Runtime.getRuntime().addShutdownHook(new Thread(() -> stop(), "Zuul-ServerGroup-JVM-shutdown-hook"));
}

代码示例来源:origin: neo4j/neo4j

public StackTracer printAtShutdown( final PrintStream out, final int interestThreshold )
{
  Runtime.getRuntime().addShutdownHook( new Thread( () -> print( out, interestThreshold ) ) );
  return this;
}

代码示例来源:origin: gocd/gocd

private Thread registerShutdownHook() {
  Thread shutdownHook = new Thread(() -> lockFile.delete());
  Runtime.getRuntime().addShutdownHook(shutdownHook);
  return shutdownHook;
}

代码示例来源:origin: stackoverflow.com

Runtime.getRuntime().addShutdownHook(new Thread() {
 public void run() { /*
   my shutdown code here
 */ }
});

代码示例来源:origin: neo4j/neo4j

private void addShutdownHook()
{
  shutdownHook = new Thread( () -> {
    log.info( "Neo4j Server shutdown initiated by request" );
    doShutdown();
  } );
  Runtime.getRuntime().addShutdownHook( shutdownHook );
}

代码示例来源:origin: gocd/gocd

private ClassLoader initSdkLoader() throws IOException {
  FileUtils.deleteQuietly(tempFolder);
  tempFolder.mkdirs();
  Runtime.getRuntime().addShutdownHook(new Thread(() -> FileUtils.deleteQuietly(tempFolder)));
  explodeNatives();
  setNativePath(tempFolder);
  return new NestedJarClassLoader(getJarURL(), "org/apache/log4j/", "org/apache/commons/logging/");
}

代码示例来源:origin: Netflix/zuul

private ServerGroup(String name, int acceptorThreads, int workerThreads, EventLoopGroupMetrics eventLoopGroupMetrics) {
  this.name = name;
  this.acceptorThreads = acceptorThreads;
  this.workerThreads = workerThreads;
  this.eventLoopGroupMetrics = eventLoopGroupMetrics;
  Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
    public void uncaughtException(final Thread t, final Throwable e) {
      LOG.error("Uncaught throwable", e);
    }
  });
  Runtime.getRuntime().addShutdownHook(new Thread(() -> stop(), "Zuul-ServerGroup-JVM-shutdown-hook"));
}

相关文章