前言: 本渣渣想分析分析Doug Lea大佬对高并发代码编写思路, 于是找到了我们今天的小主角ConcurrentLinkedQueue进行鞭打, 说实话草稿我都打好了, 就差临门一脚, 给踢折了

直接看问题, ideaDebug非Debug模式下运行结果不同, vscode复现, eclipse毫无鸭梨

怎么发现的问题?

从这段代码开始

public static void main(String[] args) throws InterruptedException, NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException, InstantiationException {
ConcurrentLinkedQueue<String> queue = new ConcurrentLinkedQueue<>();
queue.add("zhazha");
// 在下面这行下断点
Field headField = queue.getClass().getDeclaredField("head");
headField.setAccessible(true);
Object head = headField.get(queue); Field itemField = queue.getClass().getDeclaredField("ITEM");
itemField.setAccessible(true);
VarHandle ITEM = (VarHandle) itemField.get(head);
Object o = ITEM.get(head);
System.out.println(o);
}

你会发现一个神奇的现象, 如果我们下断点在Field headField = queue.getClass().getDeclaredField("head");这一行代码, 单步执行下来会发现System.out.println(o);打印出了zhazha, 但是如果不下断点, 直接运行打印null

为了防止是WARNING: An illegal reflective access operation has occurred警告的影响, 我改了改源码, 用unsafe获取试试

private static Unsafe unsafe;

static {
Class<Unsafe> unsafeClass = Unsafe.class;
Unsafe unsafe = null;
try {
Field unsafeField = unsafeClass.getDeclaredField("theUnsafe");
unsafeField.setAccessible(true);
ConcurrentLinkedQueueDemo.unsafe = (Unsafe) unsafeField.get(null);
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
} public static void main(String[] args) throws InterruptedException, NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException, InstantiationException {
ConcurrentLinkedQueue<String> queue = new ConcurrentLinkedQueue<>();
queue.add("zhazha");
// 在下面这行下断点
long headOffset = unsafe.objectFieldOffset(queue.getClass().getDeclaredField("head"));
Object head = unsafe.getObject(queue, headOffset); long itemOffset = unsafe.staticFieldOffset(ConcurrentLinkedQueue.class.getDeclaredField("ITEM"));
Object base = unsafe.staticFieldBase(ConcurrentLinkedQueue.class.getDeclaredField("ITEM"));
VarHandle ITEM = (VarHandle) unsafe.getObject(base, itemOffset); Object o = ITEM.get(head);
System.out.println(o);
}

完美复现

第一反应我的问题

去源码里看看怎么回事. 但.......这...........

仔细看红箭头的地址, tpheadtail都是同一个地址, 看上面的代码发现全是tail赋值给这三个变量的

NEXT源码

他的接收类是Node, 接收字段是next, 接收字段类型Node

看这源码的势头, NEXT修改的是p对象, 如果该对象的next节点为null, 则把newNode设置到节点上, 此时p对象指向的是tail, 同时head也是指向的tail节点, 所以这句话执行完毕, head.nexttail.next同样都是newNode节点

但.....................这.....................

head节点被直接替换掉, tail保持不变

此时我的表情应该是这样

怀疑猫生

private static Unsafe unsafe;

static {
Class<Unsafe> unsafeClass = Unsafe.class;
Unsafe unsafe = null;
try {
Field unsafeField = unsafeClass.getDeclaredField("theUnsafe");
unsafeField.setAccessible(true);
ConcurrentLinkedQueueDemo.unsafe = (Unsafe) unsafeField.get(null);
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
} public static void main(String[] args) throws InterruptedException, NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException, InstantiationException {
ConcurrentLinkedQueue<String> queue = new ConcurrentLinkedQueue<>();
queue.add("zhazha"); // 在这里下断点
Class<? extends ConcurrentLinkedQueue> queueClass = queue.getClass(); Object head = unsafe.getObject(queue, unsafe.objectFieldOffset(queueClass.getDeclaredField("head"))); Field itemField = queueClass.getDeclaredField("ITEM");
itemField.setAccessible(true);
VarHandle ITEM = (VarHandle) itemField.get(queue);
Object item = ITEM.get(head);
System.out.println(item); // zhazha long itemOffset = unsafe.staticFieldOffset(queueClass.getDeclaredField("ITEM"));
Object base = unsafe.staticFieldBase(queueClass.getDeclaredField("ITEM"));
VarHandle ITEM2 = (VarHandle) unsafe.getObject(base, itemOffset);
Object item2 = ITEM2.get(head);
System.out.println(item2); // zhazha
}

单步调试出来还是zhazha, 而且为了防止反射出了问题, 我同时用了Unsafe和反射两种方法

copy 源码添加自己的调试函数再次测试

得了得了, 放终极大招试试, copy ConcurrentLinkedQueue源码出来改成MyConcurrentLinkedQueue

offer方法添加几个输出

public boolean offer(E e) {
final Node<E> newNode = new Node<E>(Objects.requireNonNull(e)); for (Node<E> t = tail, p = t; ; ) {
Node<E> q = p.next;
if (q == null) {
if (NEXT.compareAndSet(p, null, newNode)) {
System.out.println("this.head.item = " + this.head.item);
System.out.println("this.tail.item = " + this.tail.item);
System.out.println("this.head.next.item = " + this.head.next.item);
System.out.println("this.tail.next.item = " + this.tail.next.item);
if (p != t) {
TAIL.weakCompareAndSet(this, t, newNode);
}
return true;
}
}
else if (p == q) {
p = (t != (t = tail)) ? t : head;
}
else {
p = (p != t && t != (t = tail)) ? t : q;
}
}
}

主函数就比较简单了直接

public static void main(String[] args) {
MyConcurrentLinkedQueue<String> queue = new MyConcurrentLinkedQueue<String>();
queue.add("zhazha");
}

直接在非Debug模式下运行, 发现打印出来的是

this.head.item = null
this.tail.item = null
this.head.next.item = zhazha
this.tail.next.item = zhazha Process finished with exit code 0

Debug模式下单步运行发现

this.head.item = zhazha
this.tail.item = null
Exception in thread "main" java.lang.NullPointerException
at com.zhazha.juc.MyConcurrentLinkedQueue.offer(MyConcurrentLinkedQueue.java:117)
at com.zhazha.juc.MyConcurrentLinkedQueue.add(MyConcurrentLinkedQueue.java:67)
at com.zhazha.juc.MyConcurrentLinkedQueueDemo.main(MyConcurrentLinkedQueueDemo.java:13)
Process finished with exit code 1

纳尼?

不信邪的我在NEXT cas操作的前后增加了sleep方法, 以非Debug模式下运行

this.head.item = null
this.tail.item = null
this.head.next.item = zhazha
this.tail.next.item = zhazha

还是不一样

多环境IDE测试

放终极终极终极SVIP大招 ===> 放在eclipse上试试??? 或者vscode上???

在vscode上以Debug模式单步运行输出

this.head.item = zhazha
this.tail.item = null
Exception in thread "main" java.lang.NullPointerException
at MyConcurrentLinkedQueue.offer(MyConcurrentLinkedQueue.java:116)
at MyConcurrentLinkedQueue.add(MyConcurrentLinkedQueue.java:66)
at MyConcurrentLinkedQueueDemo.main(MyConcurrentLinkedQueueDemo.java:11)

非Debug模式直接输出

this.head.item = null
this.tail.item = null
this.head.next.item = zhazha
this.tail.next.item = zhazha

在eclipse上以Debug模式单步运行输出

this.head.item = null
this.tail.item = null
this.head.next.item = zhazha
this.tail.next.item = zhazha

非Debug运行输出

this.head.item = null
this.tail.item = null
this.head.next.item = zhazha
this.tail.next.item = zhazha

发现了没有? 还是我大eclipse坚挺住了

我通过调试ConcurrentLinkedQueue发现一个IDEA的小虫子(bug), vscode复现, eclipse毫无问题的更多相关文章

  1. 发现一个c++ vector sort的bug

    在开发中遇到一个非常诡异的问题:我用vector存储了一组数据,然后调用sort方法,利用自定义的排序函数进行排序,但是一直都会段错误,在排序函数中打印参加排序的值,发现有空值,而且每次都跟同一个数据 ...

  2. 踩坑,发现一个ShardingJdbc读写分离的BUG

    ShardingJdbc 怎么处理写完数据立即读的情况的呢? 写在前面 我本地使用了两个库来做写库(ds_0_master)和读库(ds_0_salve),两个库并没有配置主从. 下面我就使用库里的 ...

  3. 发现一个animate的小应用

    <script src="jquery-1.11.1.js"></script> <script> //animate() : //第一个参数 ...

  4. 调试 lvgl 的一个例子

    发现一个新的 vector graphic 的库,用 C 写的,效果丰富,接口简单,而且是 MIT License,所以想试一试.因为它支持 framebuffer,所以,在 linux 上先走一个. ...

  5. Entity Framework 更新失败,调试后发现是AsNoTracking的原因

    public override int SaveChanges() { var changedEntities = ChangeTracker.Entries().Where(e => e.St ...

  6. 从偶然的机会发现一个mysql特性到wooyun waf绕过题

    从偶然的机会发现一个mysql特性到wooyun waf绕过题 MayIKissYou | 2015-06-19 12:00 最近在测试的时候,偶然的机会发现了一个mysql的特性, 为啥是偶然的机会 ...

  7. 【轮子】发现一个效果丰富酷炫的Android动画库

    没有什么比发现一个好轮子更让人开心的了. 这个库分分钟提高交互体验 :AndroidViewAnimations 一张图说明一切 配置和使用也相当简单 GitHub地址

  8. 学习LINQ,发现一个好的工具。LINQPad!!

    今日学习LINQ,发现一个好的工具.LINQPad!! 此工具的好处在于,不需要在程序内执行,直接只用工具测试.然后代码通过即可,速度快,简洁方便. 可以生成其LINQ查询对应的lambda和SQL语 ...

  9. 发现一个挺好用的adb logcat工具

    其实是个Notepad++插件 直接贴地址: [http://sourceforge.net/projects/androidlogger/] ============================ ...

随机推荐

  1. Centos7环境初始化

    最近在做公司的一个环境搭建的任务的时候,要用到三台Centos7服务器,在上面要预装java1.8,docker,zookeeper并且要在docker中跑一个mysql,还要部署其他的软件.由于不是 ...

  2. Java 中布尔(boolean)类型占用多少个字节

    为什么要问这个问题,首先在Java中定义的八种基本数据类型中,除了其它七种类型都有明确的内存占用字节数外,就 boolean 类型没有给出具体的占用字节数,因为对虚拟机来说根本就不存在 boolean ...

  3. 10.19 dig:域名查询工具

    dig命令 是常用的域名查询工具,可以用于测试域名系统的工作是否正常.  dig命令的参数选项及说明 @<DNS服务器地址>    指定进行城名解析的域名服务器.当不希望使用本机默认的DN ...

  4. (数据科学学习手札123)Python+Dash快速web应用开发——部署发布篇

    1 简介 这是我的系列教程Python+Dash快速web应用开发的第二十期,在上一期中我介绍了利用内网穿透的方式,将任何可以联网的电脑作为"服务器"向外临时发布你的Dash应用. ...

  5. LogBack 日志等级设置无效,原因竟然是因为这个?!

    Hello,大家好,我是楼下小黑哥~ 最近被公司派去北京出差,本以为是个轻松的差事,北京一周游~ 但是没想到第一天就是九点半下班, 大意了~ 好了,回到正题,今天来讲下最近调试项目的时候发现的一个 L ...

  6. 西门子 S7-200 通过以太网通讯模块连接MCGS 通讯

    北京华科远创科技有限研发的远创智控ETH-YC模块,以太网通讯模块型号有MPI-ETH-YC01和PPI-ETH-YC01,适用于西门子S7-200/S7-300/S7-400.SMART S7-20 ...

  7. 删除本地解压版Mysql

    1.关闭服务 以管理员身份运行cmd,使用命令net stop mysql停止服务. 2.卸载服务 使用命令mysqld -remove mysql卸载服务. 这时候在服务里已经找不到mysql服务了 ...

  8. Mobileye独创性创新

    Mobileye独创性创新 尽管存在相似之处,但Nvidia的SFF无法与Mobileye的RSS相匹配,后者是领先的AV安全模型 迈向无人驾驶的未来,Mobileye继续以新的创新引领行业,不仅将使 ...

  9. 向Relay添加算子

    向Relay添加算子 为了在Relay IR中使用TVM算子,需要在Relay中注册算子,以确保将其集成到Relay的类型系统中. 注册算子需要三个步骤: 使用RELAY_REGISTER_OPC + ...

  10. TensorFlow多元线性回归实现

    多元线性回归的具体实现 导入需要的所有软件包:   因为各特征的数据范围不同,需要归一化特征数据.为此定义一个归一化函数.另外,这里添加一个额外的固定输入值将权重和偏置结合起来.为此定义函数 appe ...