我想创建一个将使用BubbleSort方法对数组进行排序的线程,但是我遇到了一些问题。
这是我的BubbleSort类:
package thread;
import java.util.Arrays;
public class BubbleSort implements Runnable {
private int[] array;
private long start, end;
public BubbleSort(int[] array){
this.array=array;
}
public void sort(){
int j;
boolean flag = true;
int temp;
while (flag) {
flag= false;
for( j=0;j<array.length-1;j++ ){
if (array[j]>array[j+1]){
temp = array[ j ];
array[j] = array[ j+1 ];
array[j+1] = temp;
flag=true;
}
}
}
}
@Override
public void run() {
start = System.currentTimeMillis();
this.sort();
end = System.currentTimeMillis();
System.out.println(end-start);
}
public long getTime(){
return end - start;
}
@Override
public String toString(){
return Arrays.toString(array);
}
}
和主要类别:
package multisort;
import thread.BubbleSort;
public class MultiSort {
public static void main(String[] args) {
int[] x = {12,34,53,1,23,532,102,31,12,0,344,123,5422,12341,22,3410,123,342,233,12342,234432,12334};
BubbleSort bs = new BubbleSort(x);
Thread bsThread = new Thread(bs);
bsThread.start();
System.out.println(bs+"\n"+bs.getTime());
/*
bs.sort();
System.out.println(bs); it works
*/
}
}
问题是,如果我在run方法中调用sort方法,数组将不会被排序。谁可以帮助我作出回应?
您无需等待线程完成排序。无论其他线程在做什么,您都可以在阵列上sto脚!
这就像告诉您的女儿,她可以使用汽车,然后打开引擎盖并取出马达的碎片。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句