- 浏览: 1407354 次
- 性别:
- 来自: 北京
-
文章分类
- 全部博客 (346)
- linux (10)
- hbase (50)
- hadoop (23)
- java (52)
- java multi-thread (13)
- Oracle小记 (41)
- 机器学习 (12)
- 数据结构 (10)
- hadoop hive (16)
- java io (4)
- jms (1)
- web css (1)
- kafka (19)
- xml (2)
- j2ee (1)
- spring (6)
- ibatis (2)
- mysql (3)
- ext (3)
- lucene (3)
- hadoop pig (3)
- java nio (3)
- twemproxy (1)
- antlr (2)
- maven (6)
- mina (1)
- 列数据库 (1)
- oozie (2)
- mongodb (0)
- 报错 (0)
- jetty (1)
- neo4j (1)
- zookeeper (2)
- 数据挖掘 (3)
- jvm (1)
- 数据仓库 (4)
- shell (3)
- mahout (1)
- python (9)
- yarn (3)
- storm (6)
- scala (2)
- spark (5)
- tachyon (1)
最新评论
-
guokaiwhu:
赞啊!今晚遇到相同的问题,正追根溯源,就找到了博主!
hbase 报错gc wal.FSHLog: Error while AsyncSyncer sync, request close of hlog YouAr -
喁喁不止:
很清楚,有帮助。
hive常用函数 -
dsxwjhf:
Good job !!
kafka获得最新partition offset -
Locker.Xai:
参考了
freemaker教程 -
maoweiwer:
为啥EPHEMERAL_SEQUENTIAL类型的节点并没有自 ...
zookeeper 入门讲解实例 转
根据 Java API, 所谓 shutdown hook 就是已经初始化但尚未开始执行的线程对象。在
Runtime 注册后,如果 jvm 要停止前,这些 shutdown hook 便开始执行。
有什么用呢?就是在你的程序结束前,执行一些清理工作,尤其是没有用户界面的程序。
很明显,这些 shutdown hook 都是些线程对象,因此,你的清理工作要写在 run() 里。
根据 Java API,你的清理工作不能太重了,要尽快结束。但仍然可以对数据库进行操作。
package dirk.runtime; public class ShutDownHook implements Runnable { public ShutDownHook() { // register a shutdown hook for this class. // a shutdown hook is an initialzed but not started thread, which will get up and run // when the JVM is about to exit. this is used for short clean up tasks. Runtime.getRuntime().addShutdownHook(new Thread(this)); System.out.println(">>> shutdown hook registered"); } // this method will be executed of course, since it's a Runnable. // tasks should not be light and short, accessing database is alright though. public void run() { System.out.println("/n>>> About to execute: " + ShutDownHook.class.getName() + ".run() to clean up before JVM exits."); this.cleanUp(); System.out.println(">>> Finished execution: " + ShutDownHook.class.getName() + ".run()"); } // (-: a very simple task to execute void cleanUp() { for(int i=0; i < 7; i++ ) { System.out.println(i); } } /** * there're couple of cases that JVM will exit, according to the Java api doc. * typically: * 1. method called: System.exit(int) * 2. ctrl-C pressed on the console. * 3. the last non-daemon thread exits. * 4. user logoff or system shutdown. * @param args */ public static void main(String[] args) { new ShutDownHook(); System.out.println(">>> Sleeping for 5 seconds, try ctrl-C now if you like."); try { System.out.println("jvm run run run"); Thread.sleep(5000); // (-: give u the time to try ctrl-C System.out.println("jvm prepare to shutDown"); } catch (InterruptedException ie) { ie.printStackTrace(); } System.out.println(">>> Slept for 10 seconds and the main thread exited."); } }
发表评论
-
java内存使用查看 转
2015-10-29 14:51 900转:http://mxsfengg.iteye.com ... -
Java线上应用故障排查之二:高内存占用
2015-08-17 16:28 0搞Java开发的,经常会碰到下面两种异常: 1、java. ... -
java filechannel
2015-08-14 15:42 1078Java NIO中的FileChannel是一个连接到文件 ... -
Java线上应用故障排查之一:高CPU占用
2015-08-06 13:58 6219转http://blog.csdn.net/blade20 ... -
java注释
2015-04-10 15:49 0Java注解是附加在代码中的一些元信息,用于一些工具在编译、 ... -
转jvm
2015-03-24 14:13 1693一、回顾JVM内存分配 ... -
java 域名转换
2014-12-22 11:05 784import java.net.InetAddres ... -
freemaker教程
2014-10-13 11:56 2028新换了工作,与想象差距也太大了 最近沦落到做报表了,我就 ... -
protocal buffers入门实例
2014-09-22 21:08 1686hadoop yarn中新的系列化protocol buf ... -
正则小计
2014-09-18 20:47 0&site=(.*?)&可以匹配site的值 ... -
在HBase中应用MemStore-Local Allocation Buffers解决Full GC问题
2014-06-13 23:05 1635译者注:上个月 ... -
java ipc 实例
2014-05-21 22:59 4915java ipc实例,仿照hadoop ipc写的实例 1 ... -
java worker thread模式
2014-03-25 22:46 2002转两个帖子 一个java wo ... -
bloom filter
2014-03-09 19:41 1969看到hadoop join和hbase都有bloo ... -
java reference
2014-03-09 17:49 732转 http://www.iteye.com/to ... -
annotation实例
2014-02-11 22:04 1167加载指定目录的所有class,通过注释区分实体类 p ... -
java获取子类 转
2014-02-11 16:58 3160获取子类 package com.tools; ... -
动态代理
2013-08-14 20:38 1107动态代理,转:http://langyu.iteye. ... -
java byte inputstream and outputstream
2013-02-18 11:47 0转http://blog.csdn.net/rcoder ... -
JVM同步浅析
2013-01-11 11:38 1675堆 (所有类的实例或 ...
相关推荐
ShutdownHook-Java 优雅停机解决方案 ShutdownHook 是 Java 语言提供的一种钩子机制,当 JVM 接受到系统的关闭通知之后,调用 ShutdownHook 内的方法,用以完成清理操作,从而平滑的退出应用。这种有计划平滑的...
Java中得ShutdownHook提供了比较好的方案。 JDK在1.3之后提供了Java Runtime.addShutdownHook(Thread hook)方法,可以注册一个JVM关闭的钩子,这个钩子可以在以下几种场景被调用: 1)程序正常退出 2)使用...
在Java编程中,`Runtime`类的`shutdownHook`是一个重要的特性,它允许开发者在Java虚拟机(JVM)即将关闭时执行清理任务。本文将深入解析`shutdown hook`的使用,以及它在不同场景下的应用。 首先,`shutdown hook`...
在Java应用中,特别是对于SpringBoot这类框架,开发者通常会注册一个`ShutdownHook`,这是一种在JVM退出前执行的回调机制。例如,在上面的代码中,当使用`Runtime.getRuntime().addShutdownHook()`方法注册了一个新...
15. **关闭钩子(Shutdown Hook)**:通过`Runtime.getRuntime().addShutdownHook(Thread hook)`注册自定义的关闭钩子,这些钩子在JVM关闭时执行,用于清理资源。 16. **精灵线程(Daemon Thread)**:精灵线程是不...