java - 这个线程能不能存活以及如何使用java.lang.Thread.join()方法

标签 java multithreading java-6

我之前阅读过一些 SO 问题和文档,但没有找到我的回复:

所以...

public class MyThread extends Thread {
  public MyThread() {
    this.setName("MyThread-" + System.currentTimeMillis());
    this.start();
  }

  public MyThread(long millis) throws InterruptedException {
    this.setName("MyThread-" + System.currentTimeMillis());
    this.join(millis);
    this.start();
  }

  @Override
  public void run() {
    System.out.println("I am running...");
    // This thread does not sleep... no Thread.sleep() in run method.
    // Do some things like requesting a database
    // Database response happens in less time that the timeout
  }
}

public class MyClass {

  public MyClass(){
    for (int i = 0; i < 5; i++) {
      Thread t1 = new MyThread();
      t1.join(5000);
      if (t1.isAlive()) {
        System.out.println("I'm alive");
        // do some things
      } else {
        System.out.println("I'm not alive");
      }

      Thread t2 = new MyThread(5000);
      if (t2.isAlive()) {
        System.out.println("I'm alive");
        // do some things
      } else {
        System.out.println("I'm not alive");
      }

    }
  }
}

似乎不可能,但是 t1 中的一个是否还活着? t2 呢? 当我在 start()

之后调用 join() 时发生了什么

有关信息,我正在使用:

  • JVM:Java HotSpot(TM) 客户端 VM(20.45-b01,混合模式,共享)
  • Java:版本 1.6.0_45,供应商 Sun Microsystems Inc.

阅读您的一些回复后更新

如果我理解,更好的实现应该是这样的:

public class MyThread extends Thread {
  public MyThread() {
    super("MyThread-" + System.currentTimeMillis());
  }

  @Override
  public void run() {
    System.out.println("I am running...");
    // This thread does not sleep... no Thread.sleep() in run method.
    // Do some things like requesting a database
    // Database response happens in less time that the timeout
  }
}

public class MyClass {

  public MyClass(){
    for (int i = 0; i < 5; i++) {
      Thread t1 = new MyThread();
      t1.start();
      t1.join(5000);

      if (t1.isAlive()) {
        System.out.println("I'm alive");
        // do some things
      } else {
        System.out.println("I'm not alive");
      }
    }
  }
}

这两个回复对我帮助很大:https://stackoverflow.com/a/29775219/1312547https://stackoverflow.com/a/29775083/1312547

最佳答案

isAlive() 的文档说:

A thread is alive if it has been started and has not yet died.

join() 的文档说:

Waits at most millis milliseconds for this thread to die

因此,在 join() 返回后,

  • 线程已经死亡,isAlive() 将为 false
  • 或者线程还没有死但是延迟已经过去,并且 isAlive() 可能为真。这是可能的,但可能性极小,因为线程有 5 秒的时间来完成打印到控制台的简单工作。

关于java - 这个线程能不能存活以及如何使用java.lang.Thread.join()方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29774124/

相关文章:

.net - 调试多线程应用程序的建议

java - SSL 握手 - java.security.InvalidAlgorithmParameterException 问题

java - 为什么 ArrayBlockingQueue 在捕获 InterruptedException 后会发出 'not full' 信号?

java - ch.qos.logback.classic 和 slf4j-log4j12

c# - 静态类的竞争条件?

python - 使用 subprocess 模块会释放 python GIL 吗?

java - 使用pdfbox创建pdf时出错 : Identifier expected after this token

java - 解析 LIUM 说话人二值化输出

java - 如何使用MultipleOutputs <KEYOUT,VALUEOUT>将输出数据写入多个输出

没有单例的 Java