0

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.

Ian Hazzard
  • 7,661
  • 7
  • 34
  • 60
user4267868
  • 45
  • 1
  • 6

2 Answers2

1

How do I implement isAlive() or join() function when there is no real name for Thread

You cannot. ; ) When you do not assign something to a variable, you lose track of it forever. (Technically speaking, Thread is a little bit special in this regard.)

You can put your Threads in an array to keep track of them.

Thread[] threads = new Thread[20];

for (int i = 0; i < threads.length; i++) {
    threads[i] = new Thread(new mthread());
    threads[i].start();
}       

for (int i = 0; i < threads.length; i++) {
    System.out.println(i + " isAlive() = " + threads[i].isAlive());
}

try {
    for (int i = 0; i < threads.length; i++){       
        threads[i].join();           
    }
} catch (InterruptedException e) {          
    e.printStackTrace();
}
for (int i = 0; i < threads.length; i++) {
    System.out.println(i + " isAlive() = " + threads[i].isAlive());
}

As a side note, class names in Java begin with a capital letter by convention. Your mthread should be MThread. It should also probably be MRunnable since it's a Runnable, not a Thread.

Community
  • 1
  • 1
Radiodef
  • 37,180
  • 14
  • 90
  • 125
0

You'll need to store those Thread objects somewhere. ArrayLists are one option:

public static void main(String[] args) {
    List<Thread> threads = new ArrayList<>();
    for (int i = 0; i < 20; i++){
        threads.add(new Thread (new mthread()).start());
    }       

    for (int i = 0; i < 20; i++){
        **System.out.println(i + " isAlive() = " + threads.get(i).isAlive());**
    }

    try {
        for (int i = 0; i < 20; i++){       
            **threads.get(i).join();**           
        }
    } catch (InterruptedException e) {          
        e.printStackTrace();
    }
    for (int i = 0; i < 20; i++){
        **System.out.println(i + " isAlive() = " + threads.get(i).isAlive());**
    }       
}
victorantunes
  • 1,141
  • 9
  • 20