捕获用户按键"Ctrl+C"

在Console下,用户按下Ctrl+C后,进程就会收到相应的中断信号(Signal). 然后,该进程就会作手处理(或默认不处理)。

下面,看看Java中怎么处理用户按下Ctrl+C(linux下 kill -15 pid也可).

	public static void hookBySignal(){
SignalHandler handler = new SignalHandler(){
public void handle(Signal sig) {
System.out.println("hooking shutdown signal");
} };
Signal.handle(new Signal("INT"),handler);
//hook ctrl+c
Signal.handle(new Signal("TERM"),handler);
//hook alt+f4
}


[color=darkred]备注: 上面是引用sun.misc Package下的Class,非Sun的JVM,可能就不适用。[/color]


再来看看[url=http://eclipse.org/jetty/]Jetty[/url] 是怎么处理这类的?
/* ------------------------------------------------------------ */
/* ------------------------------------------------------------ */
/* ------------------------------------------------------------ */
/**
* ShutdownHook thread for stopping all servers.
*
* Thread is hooked first time list of servers is changed.
*/
private static class ShutdownHookThread extends Thread
{
private boolean hooked = false;
private ArrayList servers = new ArrayList();

/**
* Hooks this thread for shutdown.
*
* @see java.lang.Runtime#addShutdownHook(java.lang.Thread)
*/
private void createShutdownHook()
{
if (!Boolean.getBoolean("JETTY_NO_SHUTDOWN_HOOK") && !hooked)
{
try
{
Method shutdownHook = java.lang.Runtime.class.getMethod("addShutdownHook", new Class[]
{ java.lang.Thread.class});
shutdownHook.invoke(Runtime.getRuntime(), new Object[]
{ this});
this.hooked = true;
}
catch (Exception e)
{
if (Log.isDebugEnabled())
Log.debug("No shutdown hook in JVM ", e);
}
}
}

/**
* Add Server to servers list.
*/
public boolean add(Server server)
{
createShutdownHook();
return this.servers.add(server);
}

/**
* Contains Server in servers list?
*/
public boolean contains(Server server)
{
return this.servers.contains(server);
}

/**
* Append all Servers from Collection
*/
public boolean addAll(Collection c)
{
createShutdownHook();
return this.servers.addAll(c);
}

/**
* Clear list of Servers.
*/
public void clear()
{
createShutdownHook();
this.servers.clear();
}

/**
* Remove Server from list.
*/
public boolean remove(Server server)
{
createShutdownHook();
return this.servers.remove(server);
}

/**
* Remove all Servers in Collection from list.
*/
public boolean removeAll(Collection c)
{
createShutdownHook();
return this.servers.removeAll(c);
}

/**
* Stop all Servers in list.
*/
public void run()
{
setName("Shutdown");
Log.info("Shutdown hook executing");
Iterator it = servers.iterator();
while (it.hasNext())
{
Server svr = (Server) it.next();
if (svr == null)
continue;
try
{
svr.stop();
}
catch (Exception e)
{
Log.warn(e);
}
Log.info("Shutdown hook complete");

// Try to avoid JVM crash
try
{
Thread.sleep(1000);
}
catch (Exception e)
{
Log.warn(e);
}
}
}
}


注意到他为什么要使用反射,而不直接使用java.lang.Runtime.addShutdownHook ?
仔细想一下,应该主要是考虑到jdk版本兼容问题(低版本1.2.2下不被支持)。


[color=darkblue]再贴上java.lang.Runtime#addShutdownHook的[url=http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Runtime.html#addShutdownHook%28java.lang.Thread%29]注释[/url][/color]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值