日期:2014-05-20  浏览次数:20732 次

线程监控的问题
我有两个线程 A和B,我需要它俩不停的运行.

Thread A =new Thread(new ZteQZJClientFmSocket(socketServer, socketFmPort, rawDataRing));
A.start();

Thread B =new Thread(new ZteQZJClientFmSocket(socketServer, socketFmPort, rawDataRing));
B.start();

A B线程内比较复杂,有时候会断掉.我能不能再写一个线程C监控A和B的运行,一旦A和B断掉,C就让A和B重新运行起来?

问题两天没解决,请大家帮忙.

------解决方案--------------------
不好意思没写全,下面的才是:

Java code

private void start(final Object socketServer, final Object socketFmPort, final Object rawDataRing) {
    Thread thread = new Thread(new ZteQZJClientFmSocket(socketServer, socketFmPort, rawDataRing));
    thread.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
        public void uncaughtException(Thread t, Throwable e) {
            System.out.println("重新启动线程...");
            start(socketServer, socketFmPort, rawDataRing); // 调用本身
        }
    });
    thread.start();
}