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...");
}
}
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");
} else {
System.out.println("I'm not alive");
}
Thread t2 = new MyThread(5000);
if (t2.isAlive()) {
System.out.println("I'm alive");
} 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...");
}
}
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");
} else {
System.out.println("I'm not alive");
}
}
}
}
: qaru.site/questions/1583777/... qaru.site/questions/1583777/...