JUC学习笔记——共享模型之管程( 九 )


  • 如果对象虽然被多个线程访问,但没有竞争,这时偏向了线程 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

推荐阅读