这篇文章就是验证一件事:
只有多核cpu的计算机执行多个线程时才会提高效率(并行),单核cpu执行多个线程不会提高效率(并发)。
我选择的任务是:冒泡排序长度30000的int数组
单线程下
我们先测试一下单线程下这个任务的平均用时,代码如下:
public class TestSingleTask {
private static int[] arr1 = new int[30000];
static {
randomArr(arr1);
}
public static void main(String[] args) throws InterruptedException {
long startTime = System.currentTimeMillis();
/***************冒泡开始**************/
int temp = 0;
for(int i = 0 ;i< arr1.length -1; i++){
for(int j = 0; j<arr1.length-1-i; j++){
if(arr1[j]>arr1[j+1]){
temp = arr1[j];
arr1[j] = arr1[j+1];
arr1[j+1] = temp;
}
}
}
/***************冒泡结束**