亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

關于Java BlockingQueue源碼學習的一個問題

關于Java BlockingQueue源碼學習的一個問題

POPMUISE 2018-11-13 14:32:13
先附上BlockingQueue源碼take()代碼:public class ArrayBlockingQueue<E> implements BlockingQueue<E> {    final ReentrantLock lock;    //構造體中初始化lock     public ArrayBlockingQueue<E>(){        //...     }    public E take() throws InterruptedException {        final ReentrantLock lock = this.lock;  //疑惑         lock.lockInterruptibly();        try {            while (count == 0)                 notEmpty.await();            return dequeue();         } finally {             lock.unlock();         }     } }請問take方法中,第一行final ReentrantLock lock = this.lock,為什么要把全局字段lock先復制到一個局部變量中使用呢???直接使用全局final lock不可以嗎(eg. this.lock.lockInterruptibly())??為什么要多此一舉呢?
查看完整描述

1 回答

?
鳳凰求蠱

TA貢獻1825條經驗 獲得超4個贊

復制進來可以減少接下來“獲取字段”的開銷 
算是擠性能吧



變量使用n次就可以省下n-1次的開銷


stackoverflow上有相同問題。

...copying to locals produces the smallest bytecode, and for low-level code it's nice to write code that's a little closer to the machine

太底層了,忽略吧


查看完整回答
反對 回復 2018-11-13
  • 1 回答
  • 0 關注
  • 546 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號