我想出了這個驚人的結果,這是我絕對不知道的原因: 我有被縮短到了兩個方法:同步碼的性能比不同步快一個
private static final ConcurrentHashMap<Double,Boolean> mapBoolean =
new ConcurrentHashMap<Double, Boolean>();
private static final ConcurrentHashMap<Double,LinkedBlockingQueue<Runnable>> map
= new ConcurrentHashMap<Double, LinkedBlockingQueue<Runnable>>();
protected static <T> Future<T> execute(final Double id, Callable<T> call){
// where id is the ID number of each thread
synchronized(id)
{
mapBoolean.get();// then do something with the result
map.get();//the do somethign with the result
}
}
protected static <T> Future<T> executeLoosely(final Double id, Callable<T> call){
mapBoolean.get();// then do something with the result
map.get();//the do somethign with the result
}
}
在用500多個線程進行分析,每個線程調用上述每個方法400次,我發現execute(..)比executeLoosely(..)執行至少500倍,這是奇怪的,因爲executeLoosely不同步,因此更多線程可以同時處理代碼。
任何原因?
如果您首先介紹第二種方法,該怎麼辦。 – irreputable