Can this thread be live and how to use the java.lang.Thread.join () method

I read some questions and SO docs but couldn't find an answer:

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");
      }

    }
  }
}

It can't seem to be, but can one of them t1be alive? How about t2? What happens when I call join()afterstart()

For information, I use:

  • JVM: HotSpot (TM) Java Client Virtual Machine (20.45-b01, mixed mode, sharing)
  • Java: version 1.6.0_45, vendor of Sun Microsystems Inc.

Update after reading some of your answers

If I understand, a better implementation would look something like this:

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");
      }
    }
  }
}

: qaru.site/questions/1583777/... qaru.site/questions/1583777/...

+4
4

, , (JDK 7) Thread.join(long)

millis , .

this.wait , this.isAlive.

Thread.isAlive :

, .

, :

  • t1.isAlive() false, ( ).
  • t2.isAlive() true,
    • ( ), ,
    • ( )

,

, , t1 ?

t1, , , . t2, .

+1

isAlive() :

, .

join() :

,

, , join() ,

  • , isAlive() false
  • , , isAlive() . , , , 5 , .
+2

t1.isAlive() . . t1.join(5000), , . , 5 , , , .

t2.isAlive() . t2 start. , start, . . , , . , , - . t2.isAlive() , , .

+1

t1 " " t2, " ".

t2 this.join(...) , ( , @JB), join() doc

this.wait , this.isAlive.

, , .

, t1 . Thread.sleep(6000) run(), . , ( ,...), , , , join(), ", t1 ".

+1

Source: https://habr.com/ru/post/1583777/


All Articles