0

Recently I am puzzled with Java multi-threading programming and I tried to refer some open-source code. I read Android's LinkedBlockingQueue, and the implementation of the put method really confuses me.

public void put(E e) throws InterruptedException {
    if (e == null) throw new NullPointerException();
    int c = -1;
    Node<E> node = new Node<E>(e);
    final ReentrantLock putLock = this.putLock;  // #1
    final AtomicInteger count = this.count;      // #2
    putLock.lockInterruptibly();
    try {
        // ...
        while (count.get() == capacity) {
            notFull.await();
        }
        enqueue(node);
        c = count.getAndIncrement();
        if (c + 1 < capacity)
            notFull.signal();
    } finally {
        putLock.unlock();
    }
    if (c == 0)
        signalNotEmpty();
}   

It seems that in many multi-thread programming in java, it is common that using final local variables to get a reference to a class member(#1 and #2 here as an example).

But I don't understand why programmers use this trick. In this example, what if I use this.putLock to call lockInterruptibly directly rather than call lockInterruptibly after assigning it to a local variable? What's the difference between them?

Jian Guo
  • 708
  • 1
  • 9
  • 19
  • 1
    Different class, but same author, and same trick. TL;DR micro optimization, don't copy it – Michael Jun 09 '20 at 11:51
  • @Michael Thanks for the similar question's referring, I can't find the precise way to search my confusion here :D – Jian Guo Jun 09 '20 at 11:55
  • 1
    He is just copying references because it may be slightly faster. It does nothing for correctness. The code would be easier to read without this copy, so I would suggest you do not copy this style. – Michael Jun 09 '20 at 11:56
  • 1
    @Michael this code allows to see immediately that `putLock` and `count` never change during the operation, without the need to scroll to the field declaration or even when the field declaration has not been copied into a question like this. This can be seen as an advantage. – Holger Jun 09 '20 at 13:40
  • @Holger My IDE is configured to display final and non-final fields differently, which I think is a more efficient way of achieving the same thing – Michael Jun 09 '20 at 13:44
  • 1
    @Michael how do you configure your browser to do that with the code snippet as posted above? – Holger Jun 09 '20 at 13:52
  • @Holger are you suggesting optimizing your methods for readability when posted in isolation as a snippet on Stack Overflow? I personally optimize for the common use-case – Michael Jun 09 '20 at 13:55
  • 1
    @Michael your specific IDE configuration is not “the common use-case”. If you are always coding for yourself, it’s fine to assume that every reader has a color coding of final variables. But if not, the browser is just one counter-example. The default settings of most IDEs are another. I’m not suggesting that everyone should use that style. I’m suggesting to avoid absolute statements like your “the code would be easier to read without this copy” and to accept that a particular coding style may have an advantage, even if you have different priorities. – Holger Jun 09 '20 at 14:04
  • 1
    Of course, such discussions would be obsolete, if we could use the `try(…)` syntax for locks… – Holger Jun 09 '20 at 14:07

0 Answers0