测试主机配置: 为windows10 16核32G、Linux 2核2G、Linux 8核16G、
Cpu型号和核心数,下面已经列出。
在Windows10操作系统下 16核32G操作系统下
这里我们先给出结论
不同的主机配置,执行结果是不一样的,而且处理不同的任务,结果也是不一样的。所以并发执行效率不一定比串行快。
package demo1;
public class ConcurrencyTest {
public static void main(String[] args) throws InterruptedException {
int[] arrCount=new int[]{10000,100000,10000000,100000000,1000000000};
for (int i = 0; i < arrCount.length; i++) {
int count = arrCount[i];
System.out.println("并发累计执行 :"+count+"次");
concurrency(count);
serial(count);
System.out.println("----------------------------");
}
}
private static void concurrency(int count) throws InterruptedException {
long start= System.currentTimeMillis();
Thread thread=new Thread(new Runnable() {
@Override
public void run() {
int a=0;
for (long i = 0; i < count; i++) {
a+=5;
}
}
});
thread.start();
int b=0;
for (long i = 0; i < count; i++) {
b--;
}
thread.join();
long time = System.currentTimeMillis()-start;
System.out.println("concurrency :"+time+"ms,b="+b);
}
private static void serial(int count){
long start= System.currentTimeMillis();
int a=0;
for (long i = 0; i < count; i++) {
a+=5;
}
int b=0;
for (long i = 0; i < count; i++) {
b--;
}
long time = System.currentTimeMillis() - start;
System.out.println("serial :"+time+"ms,b="+b+",a="+a);
}
}