Runtime.addShutdownHook用法

一.Runtime.addShutdownHook理解

在看别人的代码时,发现其中有这个方法,便顺便梳理一下。

void java.lang.Runtime.addShutdownHook(Thread hook)

该方法用来在jvm中增加一个关闭的钩子。当程序正常退出,系统调用 System.exit方法或虚拟机被关闭时才会执行添加的shutdownHook线程。其中shutdownHook是一个已初始化但并不有启动的线程,当jvm关闭的时候,会执行系统中已经设置的所有通过方法addShutdownHook添加的钩子,当系统执行完这些钩子后,jvm才会关闭。所以可通过这些钩子在jvm关闭的时候进行内存清理、资源回收等工作。  

 

二.示例代码

 

public class TestRuntimeShutdownHook {
	public static void main(String[] args) {

		Thread shutdownHookOne = new Thread() {
			public void run() {
				System.out.println("shutdownHook one...");
			}
		};
		Runtime.getRuntime().addShutdownHook(shutdownHookOne);

		Runnable threadOne = new Runnable() {
			public void run() {
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				System.out.println("thread one doing something...");
			}
		};

		Runnable threadTwo = new Thread() {
			public void run() {
				try {
					Thread.sleep(2000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				System.out.println("thread two doing something...");
			}
		};

		threadOne.run();
		threadTwo.run();
	}
}
  

输出如下:

 

thread one doing something...
thread two doing something...

shutdownHook one...
 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Nifty是facebook公司开源的,基于netty的thrift服务端和客户端实现。 然后使用此包就可以快速发布出基于netty的高效的服务端和客户端代码。 示例: public void startServer() { // Create the handler MyService.Iface serviceInterface = new MyServiceHandler(); // Create the processor TProcessor processor = new MyService.Processor<>(serviceInterface); // Build the server definition ThriftServerDef serverDef = new ThriftServerDefBuilder().withProcessor(processor) .build(); // Create the server transport final NettyServerTransport server = new NettyServerTransport(serverDef, new NettyConfigBuilder(), new DefaultChannelGroup(), new HashedWheelTimer()); // Create netty boss and executor thread pools ExecutorService bossExecutor = Executors.newCachedThreadPool(); ExecutorService workerExecutor = Executors.newCachedThreadPool(); // Start the server server.start(bossExecutor, workerExecutor); // Arrange to stop the server at shutdown Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { try { server.stop(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } }); } Or the same thing using guice: public void startGuiceServer() { final NiftyBootstrap bootstrap = Guice.createInjector( Stage.PRODUCTION, new NiftyModule() { @Override protected void configureNifty() { // Create the handler MyService.Iface serviceInterface = new MyServiceHandler(); // Create the processor TProcessor processor = new MyService.Processor<>(serviceInterface); // Build the server definition ThriftServerDef serverDef = new ThriftServerDefBuilder().withProcessor(processor) .build(); // Bind the definition bind().toInstance(serverDef); } }).getInstance(NiftyBootstrap.class); // Start the server bootstrap.start(); // Arrange to stop the server at shutdown Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { bootstrap.stop(); } }); } 标签:Nifty

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值