Java isAlive()和join()

时间:2014-11-19 00:48:12

标签: java multithreading

我需要isAlive()join()功能方面的帮助。我了解如何以下列形式实施isAlive()join()

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

}

但如果我想做这样的事情:

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());**
    }       
}

}

如果没有真正的线程名称,我该如何实现isAlive()join()函数?

感谢。

2 个答案:

答案 0 :(得分:1)

  

如果Thread

没有真实姓名,我如何实现isAlive()或join()函数

你做不到。 ;当你没有为变量赋值时,你会永远忘记它。 (从技术上讲,Thread在这方面是littlespecial。)

您可以将Thread放在array中以跟踪它们。

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

作为旁注,Java中的类名按惯例以大写字母开头。您的mthread应为MThread。它也应该是MRunnable,因为它是Runnable,而不是Thread

答案 1 :(得分:0)

您需要在某处存储这些Thread个对象。 ArrayLists是一种选择:

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());**
    }       
}