I need some help with isAlive()
and join()
function. I understand how to implement isAlive()
and join()
in following form:
public class mthread implements Runnable{
public void run(){
for (int i = 0; i <3; i++){
System.out.println("sleep.. " + i);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
Thread a1 = new Thread (new mthread());
Thread a2 = new Thread (new mthread());
Thread a3 = new Thread (new mthread());
a1.start();
a2.start();
a3.start();
System.out.println("a1.isAlive() = " + a1.isAlive());
System.out.println("a2.isAlive() = " + a2.isAlive());
System.out.println("a3.isAlive() = " + a3.isAlive());
try {
a1.join();
a2.join();
a3.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("a1.isAlive() = " + a1.isAlive());
System.out.println("a2.isAlive() = " + a2.isAlive());
System.out.println("a3.isAlive() = " + a3.isAlive());
}
}
But if I want to do something like this:
public class mthread implements Runnable{
public void run(){
for (int i = 0; i <3; i++){
System.out.println("sleep.. " + i);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
for (int i = 0; i < 20; i++){
new Thread (new mthread()).start();
}
for (int i = 0; i < 20; i++){
**System.out.println(i + " isAlive() = " + i.isAlive());**
}
try {
for (int i = 0; i < 20; i++){
**i.join();**
}
} catch (InterruptedException e) {
e.printStackTrace();
}
for (int i = 0; i < 20; i++){
**System.out.println(i + " isAlive() = " + i.isAlive());**
}
}
}
How do I implement isAlive()
or join()
function when there is no real name for thread?
Thanks.