關於JAVA中線程同步的效能測試

來源:互聯網
上載者:User
測試案例: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.難道測試案例的問題?用例太簡單導致同步和非同步差別不大?有待進一步驗證

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.