0

有谁知道为什么线程没有立即启动?也就是说,10 分钟,20 .. 线程不启动... logger 是 java Logger 的简单装饰器...

这是代码开始:

....
log.info("client streams prepared");
messagelistener = new Thread(new Listener(input, this));
messagelistener.start();
log.info("Connection prepared");
....

并开始监听

public class Listener implements Runnable {

    private final MyLogger logger = MyLogger.getLogger();
    private final ObjectInputStream input;
    private final Connection connection;

    public Listener(ObjectInputStream input, Connection callback) {
        connection = callback;
        this.input = input;
        logger.info("Listener created");
    }

    @Override
    public void run() {
        logger.info("listener thread in connection with "+ connection.getUser().getDisplayName() + " is started");
        //.....samecode
        logger.info("listener thread in connection with "
                + connection.getUser().getDisplayName() + " is stopped");
    }
}

日志:

2013-07-29 22:36:58 MyLogger info INFO: client streams prepared  
2013-07-29 22:36:58 MyLogger info INFO: Listener created  
2013-07-29 22:36:58 MyLogger info INFO: Connection prepared

in print see info before start, and after start, but not the first line of the method run,从启动到现在已经10多分钟了,系统没有加载,我不明白为什么它不起作用?有人能够向我解释我做错了什么?

4

1 回答 1

3

...而且从启动到现在已经十多分钟了,系统没有加载,我不明白为什么它不起作用?有人能够向我解释我做错了什么?

我怀疑该run()方法正在引发某种异常——可能是 NPE。

logger.info("listener thread in connection with "+
      connection.getUser().getDisplayName() + " is started");

有没有可能connectionnullgetUser()返回 null ?我敢打赌,如果你只是记录一个简单的"made it here"方法,你会在日志中看到它。您可能应该将这些run()行包含在一个try {} catch (Exception e) { log exception }块中并记录异常。我还尝试使用调试器并在第一个日志行中放置一个断点,然后单步执行以查看发生了什么。请参阅此eclipse 调试教程

您也可以UncaughtExceptionHandler在您的线程上设置一个以这种方式记录它。请参阅:有效使用 UncaughtExceptionHandler

另一种可能性是您MyLogger正在将run()日志条目记录到另一个文件中,但看起来主线程日志消息使用相同的记录器的可能性较小。如果这是可能的,那么System.out.println("in run()");在开始时使用run()将有助于排除这种情况。

于 2013-07-29T20:55:59.163 回答