<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    莊周夢蝶

    生活、程序、未來
       :: 首頁 ::  ::  :: 聚合  :: 管理

    ReentrantLock和內部鎖的性能對比(update)

    Posted on 2007-09-14 17:15 dennis 閱讀(8124) 評論(2)  編輯  收藏 所屬分類: java
        ReentrantLock是jdk5引入的新的鎖機制,它與內部鎖(synchronize) 相同的并發性和內存語義,比如可重入加鎖語義。在中等或者更高負荷下,ReentrantLock有更好的性能,并且擁有可輪詢和可定時的請求鎖等高級功能。這個程序簡單對比了ReentrantLock公平鎖、ReentrantLock非公平鎖以及內部鎖的性能,從結果上看,非公平的ReentrantLock表現最好。內部鎖也僅僅是實現統計意義上的公平,結果也比公平的ReentrantLock好上很多。這個程序僅僅是計數,啟動N個線程,對同一個Counter進行遞增,顯然,這個遞增操作需要同步以保證原子性,采用不同的鎖來實現同步,然后查看結果。
    Counter接口:
    package net.rubyeye.concurrency.chapter13;

    public interface Counter {
        
    public long getValue();

        
    public void increment();

    }

    然后,首先使用我們熟悉的synchronize來實現同步:
    package net.rubyeye.concurrency.chapter13;

    public class SynchronizeBenchmark implements Counter {
        
    private long count = 0;

        
    public long getValue() {
            
    return count;
        }

        
    public synchronized void increment() {
            count
    ++;
        }
    }

    采用ReentrantLock的版本,切記要在finally中釋放鎖,這是與synchronize使用方式最大的不同,內部鎖jvm會自動幫你釋放鎖,而ReentrantLock需要你自己來處理。
    package net.rubyeye.concurrency.chapter13;

    import java.util.concurrent.locks.Lock;
    import java.util.concurrent.locks.ReentrantLock;

    public class ReentrantLockBeanchmark implements Counter {

        
    private volatile long count = 0;

        
    private Lock lock;

        
    public ReentrantLockBeanchmark() {
            
    // 使用非公平鎖,true就是公平鎖
            lock = new ReentrantLock(false);
        }

        
    public long getValue() {
            
    // TODO Auto-generated method stub
            return count;
        }

        
    public void increment() {
            lock.lock();
            
    try {
                count
    ++;
            } 
    finally {
                lock.unlock();
            }
        }

    }

        寫一個測試程序,使用CyclicBarrier來等待所有任務線程創建完畢以及所有任務線程計算完成,清單如下:
    package net.rubyeye.concurrency.chapter13;

    import java.util.concurrent.CyclicBarrier;

    public class BenchmarkTest {
        
    private Counter counter;

        
    private CyclicBarrier barrier;

        
    private int threadNum;

        
    public BenchmarkTest(Counter counter, int threadNum) {
            
    this.counter = counter;
            barrier 
    = new CyclicBarrier(threadNum + 1); //關卡計數=線程數+1
            this.threadNum = threadNum;
        }

        
    public static void main(String args[]) {
            
    new BenchmarkTest(new SynchronizeBenchmark(), 5000).test();
           
    //new BenchmarkTest(new ReentrantLockBeanchmark(), 5000).test();
            //new BenchmarkTest(new ReentrantLockBeanchmark(), 5000).test();   
        }

        
    public void test() {
            
    try {
                
    for (int i = 0; i < threadNum; i++) {
                    
    new TestThread(counter).start();
                }
               
    long start = System.currentTimeMillis();
                barrier.await(); // 等待所有任務線程創建
                barrier.await(); // 等待所有任務計算完成
                long end = System.currentTimeMillis();
                System.out.println(
    "count value:" + counter.getValue());
                System.out.println(
    "花費時間:" + (end - start) + "毫秒");
            } 
    catch (Exception e) {
                
    throw new RuntimeException(e);
            }
        }

        
    class TestThread extends Thread {
            
    private Counter counter;

            
    public TestThread(final Counter counter) {
                
    this.counter = counter;
            }

            
    public void run() {
                
    try {
                    barrier.await();
                    
    for (int i = 0; i < 100; i++)
                        counter.increment();
                    barrier.await();
                } 
    catch (Exception e) {
                    
    throw new RuntimeException(e);
                }
            }
        }
    }

    分別測試一下,

    將啟動的線程數限定為500,結果為:
    公平ReentrantLock:      210 毫秒
    非公平ReentrantLock :   39  毫秒
    內部鎖:                          39 毫秒

    將啟動的線程數限定為1000,結果為:
    公平ReentrantLock:      640 毫秒
    非公平ReentrantLock :   81 毫秒
    內部鎖:                           60 毫秒

    線程數不變,test方法中的循環增加到1000次,結果為:
    公平ReentrantLock:      16715 毫秒
    非公平ReentrantLock :   168 毫秒
    內部鎖:                           639  毫秒

    將啟動的線程數增加到2000,結果為:
    公平ReentrantLock:      1100 毫秒
    非公平ReentrantLock:   125 毫秒
    內部鎖:                           130 毫秒

    將啟動的線程數增加到3000,結果為:
    公平ReentrantLock:      2461 毫秒
    非公平ReentrantLock:   254 毫秒
    內部鎖:                           307 毫秒

    啟動5000個線程,結果如下:
    公平ReentrantLock:      6154  毫秒
    非公平ReentrantLock:   623   毫秒
    內部鎖:                           720 毫秒

    非公平ReentrantLock和內部鎖的差距,在jdk6上應該縮小了,據說jdk6的內部鎖機制進行了調整。

    評論

    # re: ReentrantLock和內部鎖的性能對比  回復  更多評論   

    2007-09-15 09:26 by 千里冰封
    JDK6比起JDK5確實又有些進步了,SUN公司就是牛,JAVA是越來越強大了,哈哈

    # re: ReentrantLock和內部鎖的性能對比(update)  回復  更多評論   

    2007-09-17 08:46 by dennis
    @千里冰封
    java在并發方面經過5,6兩個版本確實已經相當強大
    主站蜘蛛池模板: 亚洲人成电影亚洲人成9999网| 亚洲区不卡顿区在线观看| 亚洲成人在线网站| a毛片免费在线观看| 国产亚洲精品a在线观看| 日韩在线观看视频免费| 亚洲男人av香蕉爽爽爽爽| 成人亚洲国产精品久久| 免费乱理伦在线播放| 免费无码AV一区二区| 中文字幕亚洲乱码熟女一区二区| 国产精品福利在线观看免费不卡| 亚洲无人区午夜福利码高清完整版| 两个人的视频www免费| 亚洲va中文字幕无码久久不卡| 日韩内射激情视频在线播放免费 | 成人影片一区免费观看| 亚洲AV日韩AV永久无码绿巨人 | 日韩精品无码免费专区午夜不卡| 国产亚洲精品xxx| 最近免费中文字幕大全免费| 亚洲中文字幕在线无码一区二区| 免费无码AV电影在线观看| 国产精品亚洲精品日韩动图 | 亚洲精品国产va在线观看蜜芽| 永久免费无码网站在线观看个| 亚洲精品高清国产一线久久| 久久国产精品2020免费m3u8| 中文字幕在线观看亚洲视频| 国产午夜影视大全免费观看| 中文字字幕在线高清免费电影| 亚洲无线电影官网| 麻豆国产入口在线观看免费| 一本到卡二卡三卡免费高| 久久综合亚洲色HEZYO社区| 啦啦啦手机完整免费高清观看| 四虎影视在线看免费观看| 91在线精品亚洲一区二区| 卡一卡二卡三在线入口免费| 美女视频黄的免费视频网页 | 亚洲区日韩精品中文字幕|