5

I'm debug(Shift + F9) the method offer(E e) of JDK8 API's java.util.concurrent.ConcurrentLinkedQueue,

i found IntelliJ IDEA will change the field head of this queue soundlessly in the process of debugging,

but the run schema(Shift + F10) will not change the field, why?

and there's no code change this field in offer(E e), it makes me confused.

Then, I tried another IDE(Eclipse Mars.1 Release (4.5.1)), it hasn't such problem.

There are some result of my test:

  • run schema (Shift + F10)
head: 1159190947 
head: 1159190947 
head: 1159190947 
head: 1159190947 
  • debug schema (Step over: F8)
head: 1989972246 
head: 1791930789 
head: 1791930789 
head: 1791930789 
  • debug schema (Resume program: F9)
head: 1989972246 
head: 1989972246 
head: 1791930789 
head: 1791930789 

version info :
IntelliJ IDEA 2018.1 (Ultimate Edition)
Build #IU-181.4203.550, built on March 27, 2018
JRE: 1.8.0_162-b12 amd64
JVM: Java HotSpot(TM) 64-Bit Server VM by Oracle Corporation
Windows 10 10.0

And IntelliJ IDEA 2018.2.3 (Community Edition) & IntelliJ IDEA 2019.1 (Ultimate Edition) has the same problem.

import java.lang.reflect.Field;
import java.util.concurrent.ConcurrentLinkedQueue;

public class ConcurrentLinkedQueueTest {

    public static void main(String[] args) {
        ConcurrentLinkedQueue<String> queue = new ConcurrentLinkedQueue<>();
        print(queue);
        queue.offer("aaa");
        print(queue);
        queue.offer("bbb");
        print(queue);
        queue.offer("ccc");
        print(queue);
    }

    /**
     * 打印并发队列head属性的identityHashCode
     * @param queue
     */
    private static void print(ConcurrentLinkedQueue queue) {
        Field field = null;
        boolean isAccessible = false;
        try {
            field = ConcurrentLinkedQueue.class.getDeclaredField("head");
            isAccessible = field.isAccessible();
            if (!isAccessible) {
                field.setAccessible(true);
            }
            System.out.println("head: " + System.identityHashCode(field.get(queue)));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            field.setAccessible(isAccessible);
        }
    }
}

1 Answer 1

6

By default, IntelliJ IDEA invokes the toString() method on objects to obtain their presentation in the Watches view. In ConcurrentLinkedQueue, the toString() method iterates over the collection, which can update the head field via the updateHead() call in the first() method (and possibly in other places, I haven't investigated the entire implementation.

If you go to Preferences | Build, Execution, Deployment | Debugger | Data Views | Java and turn off "Enable toString() object view", you should no longer observe this behavior.

1
  • I turn off the "Enable toString() object view" and debug again, it makes the same problem. But you gives me the right idea, in the "Data Views" has another options "Enable alternative view for Collections classes", i turn off it too, and got the right result. Thank you very much!
    – hiccup
    Apr 29, 2019 at 3:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.