繁体   English   中英

等待所有线程在Java中完成

[英]wait for all threads complete in Java

我使用cern.colt.ParallelQuickSort库中的多线程quicksort Java函数( http://incanter.org/docs/parallelcolt/api/cern/colt/ParallelQuickSort.htmlhttps://github.com/Danimoth/Parallel-Colt /blob/master/src/cern/colt/ParallelQuickSort.java )。 我想测试选择不同数量的线程时经过了多少时间。 我使用System.nanoTime()来跟踪运行时间。 但是,即使我选择的线程数和未排序的数组在多次运行中都相同,运行时间也大不相同。 我认为这是因为cern.colt.ParallelQuickSort库中提供的quicksort()无需等待线程完成。 我想知道如何编写代码以等待所有线程完成,以便可以在库提供的函数之外测量运行时间? 像下面这样:

ParallelQuickSort qs=new ParallelQuickSort();
long startTime = System.nanoTime();
qs.quickSort(unsorted_array, 0, array_size, comp, number_threads);

//java code to wait for all threads to complete

long time_elapse= System.nanoTime() - startTime;

编辑:下面是我的代码:最初,我的代码是运行快速排序,线程号为1到15,数组大小为2 ^ 10、2 ^ 15、2 ^ 20、2 ^ 25和2 ^ 28,对于每种情况我都运行30次。 为了调试,我将代码更改为仅使用1个线程运行array size = 2 ^ 10,并运行10次。

import cern.colt.ParallelQuickSort;
import cern.colt.function.tint.IntComparator;
import cern.colt.Timer;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.System.*;
import java.util.*;


public class quick_sort {

    static void readData(int dst[], int nitems, int num) throws IOException{ 
        String s="mydata"+Integer.toString(num)+".txt";
        //System.out.println(s);
        Scanner scanner = new Scanner(new File(s));
        int i = 0;
        while(scanner.hasNextInt())
        {
            dst[i++] = scanner.nextInt();
        }

    }



public static void main(String [ ] args) throws IOException
{

    //for(int i=0; i<n;i++) dst[i]=n-i;


    /*
    System.out.println("Unsorted: ");
    for(int i=0; i<n; i++) System.out.print(dst[i]+" ");
    System.out.println(" ");
    */

    IntComparator comp=new IntComparator(){
        public int compare(int a, int b){
            if(a>b) return 1;
            else if(a<b) return -1;
            else return 0;
        }
      };




    int iter=10;
    int thread_num=1;
    //FileWriter fw = new FileWriter("out.txt");

    int num[]={10, 15, 20, 25, 28};
for(int m=0; m<1; m++){         
    for(int k=1; k<=thread_num; k++){
    long estimatedTime=0;
    for(int i=0; i<iter; i++){
        int n=1<<num[m];
        int dst[]=new int[n];
        readData(dst, n, num[m]);

        ParallelQuickSort qs=new ParallelQuickSort();

        long startTime = System.nanoTime();

        qs.quickSort(dst, 0, n, comp, k);

        long temp= System.nanoTime() - startTime;
        estimatedTime+=temp;



        System.out.println("Time="+temp*0.000001);

    }
    System.out.println(num[m]+"Wall Clock Time when thread number="+k+": "+estimatedTime/iter*0.000001);
    //fw.write(num[m]+"Wall Clock Time when thread number="+k+": "+estimatedTime/iter*0.000001+'\n');
}   


    //System.out.println("Sorted: ");
    //for(int i=0; i<n; i++) System.out.print(dst[i]+" ");
    //System.out.println(" ");




}
//fw.close();
System.out.println("Finish!");

}   

}

结果如下所示:

Time=0.755289
Time=0.632124
Time=0.502016
Time=0.502922
Time=0.100524
Time=0.076072
Time=0.073657
Time=0.073355
Time=0.074261
Time=0.076374
10Wall Clock Time when thread number=1: 0.286659
Finish!

仅在所有线程/子操作完成后,函数ParallelQuickSort.quicksort返回。 您无需手动等待所有线程完成。

这可以通过查看代码来确认(查找other.get() ),并且这是唯一合理的行为。

编辑:测试性能可能难以置信,有关详细信息,请参见Java性能测试和许多其他地方。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM