关于JAVA中线程同步的性能测试

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值