博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【代码积累】JVM shutdown hook
阅读量:4099 次
发布时间:2019-05-25

本文共 1296 字,大约阅读时间需要 4 分钟。

import org.omg.SendingContext.RunTime;public class Main extends Thread{	private volatile boolean isExit = false;	private volatile static boolean isRefresh = false;	private static Main mainserver= null;	public static final Object mainLock = new Object();		public static Main getInstance() {		if( null == mainserver ) {			synchronized(Main.class) {				if( null == mainserver ) {					mainserver = new Main();				}			}		}				return mainserver;	}		public static void main(String[] args) {		// TODO Auto-generated method stub		Main servermain = Main.getInstance();				while(!servermain.isExit) {			synchronized(mainLock) {				System.out.println("Starting server main!!!");				Runtime.getRuntime().addShutdownHook(servermain); /*hook can be registered only once.*/								try {					//System.exit(0);  /*Run this to exit JVM in order to call the hook*/					mainLock.wait();				} catch (InterruptedException e) {					// TODO Auto-generated catch block					e.printStackTrace();				}								/*refresh,set isExit false*/				if(isRefresh) {					isRefresh = false;					servermain.isExit = false;					System.out.println("Restarting main server!");				}								System.out.println("Terminate the main server.After while circle.");			}		}	}	public void run() {		System.out.println("Terminate the main server.");	}}

转载地址:http://fthii.baihongyu.com/

你可能感兴趣的文章
太赞了!美团T9终于整理出Java架构之完美设计实战开源文档
查看>>
一篇文章让你了解基于Spring的测试
查看>>
10个微服务架构设计的最佳实践
查看>>
分布式ID生成策略,我和面试官掰扯了一个小时
查看>>
逆袭大厂之路——Java程序员必备金九银十跳槽面试涨薪秘籍
查看>>
清华架构师熬夜整理,带你走进Kafka消息中间件
查看>>
GitHub上标星120k的Java进阶面试教程等!(建议收藏)
查看>>
为什么说Java程序员到了必须掌握SpringBoot的时候
查看>>
基于Rust-vmm实现Kubernetes运行时
查看>>
阿里P8架构师呕心沥血整理出这份Spring Cloud实战
查看>>
Spring Boot 如何快速集成 Redis 哨兵?
查看>>
认识 MongoDB 一篇文章就够了认识 MongoDB 一篇文章就够了
查看>>
聊聊Mysql——慢sql优化方法论
查看>>
近万服务实例稳定运行 0 故障,携程微服务架构是如何落地的?
查看>>
就这一次:TCP、IP、操作系统、Netty、算法一次性讲透
查看>>
大厂面试题中爱问的「调度算法」,20张图一举拿下
查看>>
一篇文章带你深入了解MySQL 索引相关
查看>>
GitHub上标星高达55.3Kstar的牛逼项目,附项目源代码
查看>>
你要连MySQL事务实现的基本原理都不懂,那你的面试基本凉凉
查看>>
到了2020年,技术水平到底需要达到怎样的程度才能成为顶级的阿里P8架构师
查看>>