很多時候我們都以為要想處理速度更快,那就多開幾個執行緒跑!
確實多執行緒在一定情況下比單執行緒更快。
下面的**演示序列和併發執行並累加操作的時間,請分析:下面的**併發執行一定比序列執行快嗎?當count的數量增加 1萬 -> 10萬 -> 100萬 -> 1000萬 -> 1億
1public
class
concurrencytest
1011}12
13private
static
void concurrency() throws
interruptedexception 22}
23});
24thread.start();
25int b = 0;
26for (long i = 0; i < count; i++)
29long time = system.currenttimemillis() -start;
30thread.join();
31 system.out.println("concurrency :" + time + "ms,b=" +b);32}
3334
private
static
void
serial()
40int b = 0;
41for (long i = 0; i < count; i++)
44long time = system.currenttimemillis() -start;
45 system.out.println("serial:" + time + "ms,b=" + b + ",a=" +a);46}
47 }
這是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.在數量比較小的時候,很明顯序列要比併發更快。
多執行緒一定快嗎?
package com.itmyhome.test 測試併發執行和序列執行的效率 author tracymcgrady public class concurrencytest catch interruptedexception e serial 併發執行 private static void...
多執行緒一定快嗎?
cpu通過給執行緒分配cpu時間片來實現多執行緒執行的,也就是時間片輪轉機制,cpu分配給每個執行緒的時間片非常短,所以我們才會感覺多個執行緒是同事執行的。cpu在執行執行完乙個時間片後會切換到下乙個任務,切換之前會儲存上乙個任務的狀態,以便下次切換回這個任務的時候可以載入到這個任務的狀態,所以任務...
java多執行緒一定快嗎?
先看下面的例子 總結 當count 100時,同步執行的時間較短,當count 10000時,非同步執行的時間較短。看起來,並不是非同步一定比同步快。這是因為在程式執行時,cpu會通過時間片分配演算法來迴圈執行任務,在多個任務之間不停的來回切換執行緒。當併發執行累計運算元量較小時,執行的速度會比序列...