对一组线程使用.join()会产生意外结果

时间:2014-06-24 11:46:47

标签: java multithreading

我需要同时执行一组线程并在恢复主线程之前等待它们完成。这是我的自定义类MyRunnable:

 Class MyRunnable implements Runnable{
       int value;
       MyRunnable(int value){
          this.value = value;
       }
       @Override
       public void run(){
          System.out.println("Started thread " + value);
          System.out.println("Finished thread " + value);
       }
    }
    Thread[] myThreads;
    int total = 25;
    int size = 5;

    while(total > 0){
       total -= 5;
       size = 5;
       myThreads = new Thread[size];

       for(int i = 0; i < size; i++){
          myThreads[i] = new Thread(new MyRunnable (i));
       }
       for(Thread t:myThreads){
          T.start()j
       }
       for(Thread t:myThreads){
          try{
             t.join();
          } catch(InterruptedException ex) {
             System.out.println("error");
          }
       }
       System.out.println("set finished");
    }

我需要一组五个线程一次执行,主线程只在所有线程完成执行时才继续。上面的代码不起作用,因为.join的调用似乎被忽略了。我还尝试给组中的每个线程一个随机的休眠时间,以查看意外的结果是否是由于线程的完成速度快于对.join()的调用。结果仍然相同。

1 个答案:

答案 0 :(得分:0)

我调整(在每次join调用后添加println),编译并运行你的应用程序,我看不到有join被跳过的证据。代码和输出如下。

代码:

package test;

public class ThreadTest {

    static class MyRunnable implements Runnable {
        int value;

        MyRunnable(int value) {
            this.value = value;
        }

        @Override
        public void run() {
            System.out.println("Started thread " + value);
            System.out.println("Finished thread " + value);
        }
    }

    public static void main(String[] args) {
        Thread[] myThreads;
        int total = 25;
        int size = 5;

        while (total > 0) {
            total -= 5;
            size = 5;
            myThreads = new Thread[size];

            for (int i = 0; i < size; i++) {
                myThreads[i] = new Thread(new MyRunnable(i));
            }
            for (Thread t : myThreads) {
                t.start();
            }
            for (Thread t : myThreads) {
                try {
                    t.join();
                    System.out.println("joined");
                } catch (InterruptedException ex) {
                    System.out.println("error");
                }
            }
            System.out.println("set finished");
        }
    }
}

输出:

Started thread 0
Finished thread 0
Started thread 2
Started thread 1
Finished thread 1
Finished thread 2
Started thread 3
Finished thread 3
joined
joined
joined
joined
Started thread 4
Finished thread 4
joined
set finished
Started thread 0
Started thread 1
Started thread 2
Finished thread 2
Started thread 3
Finished thread 0
Finished thread 3
Finished thread 1
joined
joined
joined
joined
Started thread 4
Finished thread 4
joined
set finished
Started thread 0
Finished thread 0
Started thread 1
Started thread 2
Finished thread 1
Finished thread 2
joined
Started thread 4
Finished thread 4
Started thread 3
joined
joined
Finished thread 3
joined
joined
set finished
Started thread 0
Started thread 3
Finished thread 3
Started thread 4
Started thread 1
Started thread 2
Finished thread 1
Finished thread 4
Finished thread 0
Finished thread 2
joined
joined
joined
joined
joined
set finished
Started thread 0
Started thread 1
Finished thread 0
Finished thread 1
Started thread 2
Finished thread 2
Started thread 4
joined
joined
joined
Started thread 3
Finished thread 4
Finished thread 3
joined
joined
set finished