Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 并行气泡排序性能_Java_Multithreading_Performance_Algorithm_Sorting - Fatal编程技术网

Java 并行气泡排序性能

Java 并行气泡排序性能,java,multithreading,performance,algorithm,sorting,Java,Multithreading,Performance,Algorithm,Sorting,我的目标是实现排序算法泡泡排序使用线程,该程序似乎运行正常。但是性能很糟糕,所以我想知道如何让代码运行得更快,为什么它运行得如此糟糕 该代码基于以下算法: 并行气泡排序(A)-算法 > 1.For k = 0 to n-2 > 2.If k is even then > 3. for i = 0 to (n/2)-1 do in parallel > 4. if A[2i] > A[2i+1] then > 5. Exchange A[

我的目标是实现排序算法泡泡排序使用线程,该程序似乎运行正常。但是性能很糟糕,所以我想知道如何让代码运行得更快,为什么它运行得如此糟糕

该代码基于以下算法:

并行气泡排序(A)-算法

> 1.For k = 0 to n-2
> 2.If k is even then
> 3.  for i = 0 to (n/2)-1 do in parallel
> 4.     if A[2i] > A[2i+1] then
> 5.        Exchange A[2i] <-> A[2i+1]
> 6.Else
> 7.  for i = 0 to (n/2)-2 do in parallel
> 8.   if A[2i+1] > A[2i+2] then
> 9.     Exchange A[2i+1] <-> A[2i+2]
> 10.Next k
>1.对于k=0到n-2
>2.如果k是偶数,那么
> 3.  对于i=0至(n/2)-1个do并联
> 4.     如果A[2i]>A[2i+1],则
> 5.        交换[2i]A[2i+1]
>6.其他
> 7.  对于i=0至(n/2)-2个do并联
> 8.   如果A[2i+1]>A[2i+2],则
> 9.     交换A[2i+1]A[2i+2]
>10.下一个k

publicstaticvoidsort(){
int n=input.length;//要排序的数组的长度
线程[]thr1=新线程[(int)n/2];
线程[]thr2=新线程[(int)n/2];
int count1;
int count2;

对于(int i=0;i它如此缓慢的原因是调用
new Thread()
非常昂贵。启动另一个线程执行部分排序比在原始线程中执行所有排序需要数千倍的时钟周期


此外,即使
new Thread()
没有那么贵,您也不会看到太多(或任何)性能改进,因为排序是一个内存受限的操作,您很可能试图在一个CPU、多核系统上运行它,但CPU只有一条地址总线和一条数据总线,因此核心将主要等待彼此放开数据总线。

可能是因为气泡排序是一个
O(n^2)
算法。它总是很慢(尽管慢是一个相对术语).是的,但顺序版本实际上比并行版本运行得快。我猜是线程的开销是个问题。有很多对象等等。我明白了…所以解决方案应该是使用线程池?或Fork-join?是的,一定要尝试使用线程池,不过如果你想要我的预测,它可能会改变性能从“可怕”到“糟糕”。请让我们知道测试结果,我很好奇。
public static void sort(){
    int n = input.length; //length of the array to sort
    Thread[] thr1 = new Thread[(int)n/2];
    Thread[] thr2 = new Thread[(int)n/2];
    int count1;
    int count2;

    for(int i = 0; i<n-1;i++){
        if(i % 2 == 0){ // i even
        count1 = 0;

        for(int j = 0; j<n/2; j++){
            final int tmp = j;
            count1++;
            thr1[tmp] = new Thread(){ 
                public void run(){
                    if (input[2*tmp]>input[2*tmp+1]) 
                        swap(2*tmp,2*tmp+1);
                }
            };
            thr1[tmp].start();
        }
        //waiting for threads to finish
        for(int m = 0; m<count1; m++){
            try {
                thr1[m].join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }}
        }   
        else{ // i odd
        count2 = 0;
        for(int k = 0; k<n/2-1;k++){
            final int tmp = k;
            count2++;
            thr2[tmp] = new Thread(){
                public void run(){
                    if (input[2*tmp+1]>input[2*tmp+2])
                        swap(2*tmp+1,2*tmp+2);
                }
            };
            thr2[tmp].start();
        }
        // Waiting for threads to finish
        for(int m = 0; m<count2; m++){
            try {
                thr2[m].join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }}          
    }
}
public static void sort(){
    int n = input.length; //length of the array to sort
    ExecutorService executor = Executors.newFixedThreadPool(8);

    for(int i = 0; i<n-1;i++){
        if(i % 2 == 0){ // i even

        for(int j = 0; j<n/2; j++){
            final int tmp = j;
            executor.submit(new Runnable(){ 
                public void run(){
                    if (input[2*tmp]>input[2*tmp+1]) 
                        swap(2*tmp,2*tmp+1);}
            });}
        }   

        else{ // i odd
        for(int k = 0; k<n/2-1;k++){
            final int tmp = k;
            executor.submit(new Runnable(){
                public void run(){
                    if (input[2*tmp+1]>input[2*tmp+2])
                        swap(2*tmp+1,2*tmp+2);}
            });}
        }       
    }
executor.shutdown();
try {
    executor.awaitTermination(1, TimeUnit.DAYS);
} catch (InterruptedException e) {
    e.printStackTrace();}
}