测试用例:
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.难道测试用例的问题?用例太简单导致同步和非同步差别不大?有待进一步验证