【问题标题】:java Thread doesn't run after 15minutes15分钟后java线程不运行
【发布时间】:2013-07-29 19:53:49
【问题描述】:

有谁知道为什么线程没有立即启动?也就是说,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多分钟了,系统没有加载,我不明白为什么它不起作用? 有人能够向我解释我做错了什么吗?

【问题讨论】:

  • 将 logger.info 替换为 System.out.println 只是为了测试一下
  • 你能告诉我们整个方法,包括创建监听器的声明。另外,我们能看到Connection#getUser()的声明吗?你有可能陷入僵局吗?可能getUser()就是synchronized,创建方法也是?

标签: java multithreading


【解决方案1】:

...启动已经10多分钟了,系统没有加载,我不明白为什么它不工作?有人能够向我解释我做错了什么吗?

我怀疑 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 } 块中并记录异常。我还尝试使用调试器并在第一个日志行中放置一个断点,然后单步执行以查看发生了什么。请参阅此debugging tutorial for eclipse

你也可以在你的线程上设置一个UncaughtExceptionHandler 来记录它。见:Using UncaughtExceptionHandler effectively

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

【讨论】:

  • @user1841779 有哪些文件?线程 run() 方法内部没有隐式 try/catch。并且没有隐式记录异常。你必须自己做。
  • 我现在发现了一个错误,正如你所说的可以getUser可能会返回null,并且一直无法登录到logger,以后我将使用设置UncaughtExceptionHandler的线程...跨度>
猜你喜欢
  • 1970-01-01
  • 2018-08-17
  • 1970-01-01
  • 2016-01-04
  • 1970-01-01
  • 2023-03-12
  • 1970-01-01
  • 2022-08-18
  • 1970-01-01
相关资源
最近更新 更多