很多时候我们都以为要想处理速度更快,那就多开几个线程跑! 确实多线程在一定情况下比单线程更快。
下面的代码演示串行和并发执行并累加操作的时间,请分析:当count的数量增加 1万 -> 10万 -> 100万 -> 1000万 -> 1亿,下面的代码并发执行一定比串行执行快吗?
public class ConcurrencyTest {
private static final long count = 10000l;
public static void main(String[] args) throws InterruptedException {
for(int i=0; i<10; i++) {
concurrency();
serial();
System.out.println("------------------------------------------");
}
}
private static void concurrency() 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--;
}
long time = System.currentTimeMillis() - start;
thread.join();
System.out.println("concurrency :" + time + "ms,b=" + b);
}
private static void serial() {
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);
}
}
这是count=1万的结果,基本上串行要快些
并发 :1ms,b=-10000
串行:0ms,b=-10000,a=50000
------------------------------------------
并发 :0ms,b=-10000
串行:1ms,b=-10000,a=50000
------------------------------------------
并发 :0ms,b=-10000
串行:1ms,b=-10000,a=50000
------------------------------------------
并发 :0ms,b=-10000
串行:0ms,b=-10000,a=50000
------------------------------------------
并发 :1ms,b=-10000
串行:0ms,b=-10000,a=50000
------------------------------------------
并发 :1ms,b=-10000
串行:0ms,b=-10000,a=50000
------------------------------------------
并发 :0ms,b=-10000
串行:1ms,b=-10000,a=50000
------------------------------------------
并发 :0ms,b=-10000
串行:0ms,b=-10000,a=50000
------------------------------------------
并发 :1ms,b=-10000
串行:0ms,b=-10000,a=50000
------------------------------------------
这是count=10万l的结果,基本上串行要快点点
并发 :4ms,b=-100000
串行:3ms,b=-100000,a=500000
------------------------------------------
并发 :0ms,b=-100000
串行:1ms,b=-100000,a=500000
------------------------------------------
并发 :1ms,b=-100000
串行:0ms,b=-100000,a=500000
------------------------------------------
并发 :0ms,b=-100000
串行:1ms,b=-100000,a=500000
------------------------------------------
并发 :1ms,b=-100000
串行:0ms,b=-100000,a=500000
------------------------------------------
并发 :0ms,b=-100000
串行:0ms,b=-100000,a=500000
------------------------------------------
并发 :1ms,b=-100000
串行:0ms,b=-100000,a=500000
------------------------------------------
并发 :0ms,b=-100000
串行:0ms,b=-100000,a=500000
------------------------------------------
并发 :0ms,b=-100000
串行:1ms,b=-100000,a=500000
------------------------------------------
并发 :0ms,b=-100000
串行:0ms,b=-100000,a=500000
------------------------------------------
这是count=100万l的结果,基本上并发就要比串行要快了
并发 :5ms,b=-1000000
串行:5ms,b=-1000000,a=5000000
------------------------------------------
并发 :3ms,b=-1000000
串行:7ms,b=-1000000,a=5000000
------------------------------------------
并发 :1ms,b=-1000000
串行:7ms,b=-1000000,a=5000000
------------------------------------------
并发 :1ms,b=-1000000
串行:2ms,b=-1000000,a=5000000
------------------------------------------
并发 :1ms,b=-1000000
串行:2ms,b=-1000000,a=5000000
------------------------------------------
并发 :2ms,b=-1000000
串行:2ms,b=-1000000,a=5000000
------------------------------------------
并发 :1ms,b=-1000000
串行:3ms,b=-1000000,a=5000000
------------------------------------------
并发 :1ms,b=-1000000
串行:2ms,b=-1000000,a=5000000
------------------------------------------
并发 :1ms,b=-1000000
串行:3ms,b=-1000000,a=5000000
------------------------------------------
并发 :1ms,b=-1000000
串行:2ms,b=-1000000,a=5000000
------------------------------------------
余下的读者可以自己去调试哦。
1.在数量比较小的时候,很明显串行要比并发更快。
2.因为数量较小的时候,并发会花费很多时间在线程调度上。