測試案例:package com.gftech.dp.run.test;
/** *//**
* 對線程同步進行效能測試
* @author sinboy
* @since 2007.3.23
*
*/
public class SyncThreadTest ...{
static int count;
static final int CIRCLE_COUNT=10000;
static long start= System.currentTimeMillis();
public static void main(String[] args) ...{
for(int i=0;i<CIRCLE_COUNT;i++)
test(i);
}
public static void test(final int seed)...{
Thread thread=new Thread()...{
public void run()...{
int radom=radom(seed);
count++;
if(count==CIRCLE_COUNT)
System.out.println("time:"+(System.currentTimeMillis()-start)+"ms");
}
};
thread.start();
}
public static synchronized int radom(int seed)...{
long result = 0;
if (seed != 0) ...{
double d = Math.random();
String temp = d + "";
// System.out.println("temp:" + temp);
int len = temp.length() - 2;// 去掉開頭兩位
d = d * Math.pow(10, len);
// System.out.println("d:" + d);
result = (long) d % seed;
}
return (int) result;
}
}
測試結果(對radom方法進行同步和非同步測試):
CIRCLE_COUNT 非同步(ms) 同步(ms)
100 47 47
1000 328 360
10000 2469 2515
100000 22545 26452
結果分析:
1.但從測試結果來看,同步和非同步的已耗用時間幾道差不多
2.按道理同步會比非同步用的時間多很多才對,據有關文章分析同步比非同步慢50-100倍,可測試結果為何沒有印證這一點呢?
3.難道測試案例的問題?用例太簡單導致同步和非同步差別不大?有待進一步驗證