- 如果对象虽然被多个线程访问,但没有竞争,这时偏向了线程 T1 的对象仍有机会重新偏向 T2,重偏向会重置对象 的 Thread ID
- 当撤销偏向锁阈值超过 20 次后,jvm 会这样觉得偏向对象错误,于是会在给这些对象加锁时重新偏向至加锁线程
/*主代码*/private static void test3() throws InterruptedException {Vector<Dog> list = new Vector<>();Thread t1 = new Thread(() -> {for (int i = 0; i < 30; i++) {Dog d = new Dog();list.add(d);synchronized (d) {log.debug(i + "\t" + ClassLayout.parseInstance(d).toPrintableSimple(true));}}synchronized (list) {list.notify();}}, "t1");t1.start();Thread t2 = new Thread(() -> {synchronized (list) {try {list.wait();} catch (InterruptedException e) {e.printStackTrace();}}log.debug("===============> ");for (int i = 0; i < 30; i++) {Dog d = list.get(i);log.debug(i + "\t" + ClassLayout.parseInstance(d).toPrintableSimple(true));synchronized (d) {log.debug(i + "\t" + ClassLayout.parseInstance(d).toPrintableSimple(true));}log.debug(i + "\t" + ClassLayout.parseInstance(d).toPrintableSimple(true));}}, "t2");t2.start();}/*结果*/// 我们会发现,最开始都是101,这里设置了偏向锁[t1] - 0 00000000 00000000 00000000 00000000 00011111 11110011 11100000 00000101[t1] - 1 00000000 00000000 00000000 00000000 00011111 11110011 11100000 00000101[t1] - 2 00000000 00000000 00000000 00000000 00011111 11110011 11100000 00000101[t1] - 3 00000000 00000000 00000000 00000000 00011111 11110011 11100000 00000101[t1] - 4 00000000 00000000 00000000 00000000 00011111 11110011 11100000 00000101[t1] - 5 00000000 00000000 00000000 00000000 00011111 11110011 11100000 00000101[t1] - 6 00000000 00000000 00000000 00000000 00011111 11110011 11100000 00000101[t1] - 7 00000000 00000000 00000000 00000000 00011111 11110011 11100000 00000101[t1] - 8 00000000 00000000 00000000 00000000 00011111 11110011 11100000 00000101[t1] - 9 00000000 00000000 00000000 00000000 00011111 11110011 11100000 00000101[t1] - 10 00000000 00000000 00000000 00000000 00011111 11110011 11100000 00000101[t1] - 11 00000000 00000000 00000000 00000000 00011111 11110011 11100000 00000101[t1] - 12 00000000 00000000 00000000 00000000 00011111 11110011 11100000 00000101[t1] - 13 00000000 00000000 00000000 00000000 00011111 11110011 11100000 00000101[t1] - 14 00000000 00000000 00000000 00000000 00011111 11110011 11100000 00000101[t1] - 15 00000000 00000000 00000000 00000000 00011111 11110011 11100000 00000101[t1] - 16 00000000 00000000 00000000 00000000 00011111 11110011 11100000 00000101[t1] - 17 00000000 00000000 00000000 00000000 00011111 11110011 11100000 00000101[t1] - 18 00000000 00000000 00000000 00000000 00011111 11110011 11100000 00000101[t1] - 19 00000000 00000000 00000000 00000000 00011111 11110011 11100000 00000101[t1] - 20 00000000 00000000 00000000 00000000 00011111 11110011 11100000 00000101[t1] - 21 00000000 00000000 00000000 00000000 00011111 11110011 11100000 00000101[t1] - 22 00000000 00000000 00000000 00000000 00011111 11110011 11100000 00000101[t1] - 23 00000000 00000000 00000000 00000000 00011111 11110011 11100000 00000101[t1] - 24 00000000 00000000 00000000 00000000 00011111 11110011 11100000 00000101[t1] - 25 00000000 00000000 00000000 00000000 00011111 11110011 11100000 00000101[t1] - 26 00000000 00000000 00000000 00000000 00011111 11110011 11100000 00000101[t1] - 27 00000000 00000000 00000000 00000000 00011111 11110011 11100000 00000101[t1] - 28 00000000 00000000 00000000 00000000 00011111 11110011 11100000 00000101[t1] - 29 00000000 00000000 00000000 00000000 00011111 11110011 11100000 00000101[t2] - ===============> // 我们会发现这里从101偏向锁撤销,然后变为000无锁,然后变为001轻量级锁[t2] - 0 00000000 00000000 00000000 00000000 00011111 11110011 11100000 00000101[t2] - 0 00000000 00000000 00000000 00000000 00100000 01011000 11110111 00000000[t2] - 0 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000001[t2] - 1 00000000 00000000 00000000 00000000 00011111 11110011 11100000 00000101[t2] - 1 00000000 00000000 00000000 00000000 00100000 01011000 11110111 00000000[t2] - 1 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000001[t2] - 2 00000000 00000000 00000000 00000000 00011111 11110011 11100000 00000101[t2] - 2 00000000 00000000 00000000 00000000 00100000 01011000 11110111 00000000[t2] - 2 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000001[t2] - 3 00000000 00000000 00000000 00000000 00011111 11110011 11100000 00000101[t2] - 3 00000000 00000000 00000000 00000000 00100000 01011000 11110111 00000000[t2] - 3 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000001[t2] - 4 00000000 00000000 00000000 00000000 00011111 11110011 11100000 00000101[t2] - 4 00000000 00000000 00000000 00000000 00100000 01011000 11110111 00000000[t2] - 4 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000001[t2] - 5 00000000 00000000 00000000 00000000 00011111 11110011 11100000 00000101[t2] - 5 00000000 00000000 00000000 00000000 00100000 01011000 11110111 00000000[t2] - 5 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000001[t2] - 6 00000000 00000000 00000000 00000000 00011111 11110011 11100000 00000101[t2] - 6 00000000 00000000 00000000 00000000 00100000 01011000 11110111 00000000[t2] - 6 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000001[t2] - 7 00000000 00000000 00000000 00000000 00011111 11110011 11100000 00000101[t2] - 7 00000000 00000000 00000000 00000000 00100000 01011000 11110111 00000000[t2] - 7 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000001[t2] - 8 00000000 00000000 00000000 00000000 00011111 11110011 11100000 00000101[t2] - 8 00000000 00000000 00000000 00000000 00100000 01011000 11110111 00000000[t2] - 8 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000001[t2] - 9 00000000 00000000 00000000 00000000 00011111 11110011 11100000 00000101[t2] - 9 00000000 00000000 00000000 00000000 00100000 01011000 11110111 00000000[t2] - 9 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000001[t2] - 10 00000000 00000000 00000000 00000000 00011111 11110011 11100000 00000101[t2] - 10 00000000 00000000 00000000 00000000 00100000 01011000 11110111 00000000[t2] - 10 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000001[t2] - 11 00000000 00000000 00000000 00000000 00011111 11110011 11100000 00000101[t2] - 11 00000000 00000000 00000000 00000000 00100000 01011000 11110111 00000000[t2] - 11 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000001[t2] - 12 00000000 00000000 00000000 00000000 00011111 11110011 11100000 00000101[t2] - 12 00000000 00000000 00000000 00000000 00100000 01011000 11110111 00000000[t2] - 12 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000001[t2] - 13 00000000 00000000 00000000 00000000 00011111 11110011 11100000 00000101[t2] - 13 00000000 00000000 00000000 00000000 00100000 01011000 11110111 00000000[t2] - 13 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000001[t2] - 14 00000000 00000000 00000000 00000000 00011111 11110011 11100000 00000101[t2] - 14 00000000 00000000 00000000 00000000 00100000 01011000 11110111 00000000[t2] - 14 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000001[t2] - 15 00000000 00000000 00000000 00000000 00011111 11110011 11100000 00000101[t2] - 15 00000000 00000000 00000000 00000000 00100000 01011000 11110111 00000000[t2] - 15 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000001[t2] - 16 00000000 00000000 00000000 00000000 00011111 11110011 11100000 00000101[t2] - 16 00000000 00000000 00000000 00000000 00100000 01011000 11110111 00000000[t2] - 16 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000001[t2] - 17 00000000 00000000 00000000 00000000 00011111 11110011 11100000 00000101[t2] - 17 00000000 00000000 00000000 00000000 00100000 01011000 11110111 00000000[t2] - 17 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000001[t2] - 18 00000000 00000000 00000000 00000000 00011111 11110011 11100000 00000101[t2] - 18 00000000 00000000 00000000 00000000 00100000 01011000 11110111 00000000[t2] - 18 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000001// 在这里超过阈值之后,直接将该锁的偏向对象设置为t2线程,所有锁都设置为了101偏向锁[t2] - 19 00000000 00000000 00000000 00000000 00011111 11110011 11100000 00000101[t2] - 19 00000000 00000000 00000000 00000000 00011111 11110011 11110001 00000101[t2] - 19 00000000 00000000 00000000 00000000 00011111 11110011 11110001 00000101[t2] - 20 00000000 00000000 00000000 00000000 00011111 11110011 11100000 00000101[t2] - 20 00000000 00000000 00000000 00000000 00011111 11110011 11110001 00000101[t2] - 20 00000000 00000000 00000000 00000000 00011111 11110011 11110001 00000101[t2] - 21 00000000 00000000 00000000 00000000 00011111 11110011 11100000 00000101[t2] - 21 00000000 00000000 00000000 00000000 00011111 11110011 11110001 00000101[t2] - 21 00000000 00000000 00000000 00000000 00011111 11110011 11110001 00000101[t2] - 22 00000000 00000000 00000000 00000000 00011111 11110011 11100000 00000101[t2] - 22 00000000 00000000 00000000 00000000 00011111 11110011 11110001 00000101[t2] - 22 00000000 00000000 00000000 00000000 00011111 11110011 11110001 00000101[t2] - 23 00000000 00000000 00000000 00000000 00011111 11110011 11100000 00000101[t2] - 23 00000000 00000000 00000000 00000000 00011111 11110011 11110001 00000101[t2] - 23 00000000 00000000 00000000 00000000 00011111 11110011 11110001 00000101[t2] - 24 00000000 00000000 00000000 00000000 00011111 11110011 11100000 00000101[t2] - 24 00000000 00000000 00000000 00000000 00011111 11110011 11110001 00000101[t2] - 24 00000000 00000000 00000000 00000000 00011111 11110011 11110001 00000101[t2] - 25 00000000 00000000 00000000 00000000 00011111 11110011 11100000 00000101[t2] - 25 00000000 00000000 00000000 00000000 00011111 11110011 11110001 00000101[t2] - 25 00000000 00000000 00000000 00000000 00011111 11110011 11110001 00000101[t2] - 26 00000000 00000000 00000000 00000000 00011111 11110011 11100000 00000101[t2] - 26 00000000 00000000 00000000 00000000 00011111 11110011 11110001 00000101[t2] - 26 00000000 00000000 00000000 00000000 00011111 11110011 11110001 00000101[t2] - 27 00000000 00000000 00000000 00000000 00011111 11110011 11100000 000001
推荐阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- Seata Server 1.5.2 源码学习
- 2022极端高温!机器学习如何预测森林火灾?? 万物AI
- 1.nginx学习
- 常用Python库整理
- 图学习参考资料 词向量word2vec
- 五 RK3568开发笔记:在虚拟机上使用SDK编译制作uboot、kernel和ubuntu镜像
- realme笔记本最新消息_realme笔记本即将发布
- JUC学习笔记——进程与线程
- Seata 1.5.2 源码学习
- 第4版 高性能MySQL 第一章 MySQL架构 读书笔记