最近项目中创建线程较多,有时候会存在线程死掉的情况,所以写了一个监听线程,监听线程的存活状态
public class ThreadMonitor implements Runnable {
private static final Logger log = LoggerFactory.getLogger(ThreadMonitor.class);
/**volatile关键字防止指令重排*/
private volatile static ThreadMonitor monitor = null;
/**线程安全*/
private ConcurrentLinkedQueue<Thread> threadList = new ConcurrentLinkedQueue<>();
private ThreadMonitor() {
}
/**
* 单例模式,全局只产生一个对象
*
* @return
*/
public static ThreadMonitor getInstance() {
if (monitor == null) {
synchronized (ThreadMonitor.class) {
if (monitor == null) {
monitor = new ThreadMonitor();
}
}
}
return monitor;
}
public void addThreadMonitor(Thread thread) {
threadList.add(thread);
}
&#