A disc synchronized (II) - and biased toward the lock Batch Batch weight revocation

Before this paper to explain, first take a brief look at why there is heavy bias batch and batch revoked.
 
Batch weight bias: When a thread creates a large number of objects and perform the initial synchronization, and later another thread to these objects also operates as a lock object, will lead the heavy bias toward the operation of the lock.
Batch revoked: In a multi-threaded intense competition, the lock will reduce the efficiency of the use of biased, and thus produce a batch revocation mechanism.
 

 JVM's default parameter values

By default parameter value of the JVM, look toward the weight and bulk batch withdrawal threshold.
Set JVM parameters -XX: + PrintFlagsFinal, at the start of the project output to the JVM the default parameter values
 
intx BiasedLockingBulkRebiasThreshold = 20 default batch heavy bias biased locking threshold
intx BiasedLockingBulkRevokeThreshold = 40 default threshold biased locking batch revocation
Of course, we can -XX: manually set the threshold BiasedLockingBulkRevokeThreshold: BiasedLockingBulkRebiasThreshold and -XX

 

Batch weight bias

public static void main(String[] args) throws Exception {
        //延时产生可偏向对象
        Thread.sleep(5000);

        //创造100个偏向线程t1的偏向锁
        List<A> listA = new ArrayList<>();
        Thread t1 = new Thread(() -> {
            for (int i = 0; i <100 ; i++) {
                A a = new A();
                synchronized (a){
                    listA.add(a);
                }
            }
            try {
                //为了防止JVM线程复用,在创建完对象后,保持线程t1状态为存活
                Thread.sleep(100000000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        t1.start();

        //睡眠3s钟保证线程t1创建对象完成
        Thread.sleep(3000);
        out.println("打印t1线程,list中第20个对象的对象头:");
        out.println((ClassLayout.parseInstance(listA.get(19)).toPrintable()));

        //创建线程t2竞争线程t1中已经退出同步块的锁
        Thread t2 = new Thread(() -> {
            //这里面只循环了30次!!!
            for (int i = 0; i < 30; i++) {
                A a =listA.get(i);
                synchronized (a){
                    //分别打印第19次和第20次偏向锁重偏向结果
                    if(i==18||i==19){
                        out.println("第"+ ( i + 1) + "次偏向结果");
                        out.println((ClassLayout.parseInstance(a).toPrintable()));
                    }
                }
            }
            try {
                Thread.sleep(10000000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        t2.start();

        Thread.sleep(3000);
        out.println("打印list中第11个对象的对象头:");
        out.println((ClassLayout.parseInstance(listA.get(10)).toPrintable()));
        out.println("打印list中第26个对象的对象头:");
        out.println((ClassLayout.parseInstance(listA.get(25)).toPrintable()));
        out.println("打印list中第41个对象的对象头:");
        out.println((ClassLayout.parseInstance(listA.get(40)).toPrintable()));
    }
 
我们一步一步来分析打印结果
首先,创造了100个偏向线程t1的偏向锁,结果没什么好说的,100个偏向锁嘛,偏向的线程ID信息为531939333

 

再来看看重偏向的结果,线程t2,前19次偏向均产生了轻量锁,而到第20次的时候,达到了批量重偏向的阈值20,此时锁并不是轻量级锁,而变成了偏向锁,此时偏向的线程t2,线程t2的ID信息为5322162821

 

最后我们再来看一下偏向结束后的对象头信息。
前20个对象,并没有触发了批量重偏向机制,线程t2执行释放同步锁后,转变为无锁形态
第20~30个对象,触发了批量重偏向机制,对象为偏向锁状态,偏向线程t2,线程t2的ID信息为5322162821
而31个对象之后,也没有触发了批量重偏向机制,对象仍偏向线程t1,线程t1的ID信息为531939333

 

批量撤销

 
我们再来看看批量撤销
public static void main(String[] args) throws Exception {

    Thread.sleep(5000);
    List<A> listA = new ArrayList<>();

    Thread t1 = new Thread(() -> {
        for (int i = 0; i <100 ; i++) {
            A a = new A();
            synchronized (a){
                listA.add(a);
            }
        }
        try {
            Thread.sleep(100000000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    });
    t1.start();
    Thread.sleep(3000);

    Thread t2 = new Thread(() -> {
        //这里循环了40次。达到了批量撤销的阈值
        for (int i = 0; i < 40; i++) {
            A a =listA.get(i);
            synchronized (a){
            }
        }
        try {
            Thread.sleep(10000000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    });
    t2.start();

    //———————————分割线,前面代码不再赘述——————————————————————————————————————————
    Thread.sleep(3000);
    out.println("打印list中第11个对象的对象头:");
    out.println((ClassLayout.parseInstance(listA.get(10)).toPrintable()));
    out.println("打印list中第26个对象的对象头:");
    out.println((ClassLayout.parseInstance(listA.get(25)).toPrintable()));
    out.println("打印list中第90个对象的对象头:");
    out.println((ClassLayout.parseInstance(listA.get(89)).toPrintable()));


    Thread t3 = new Thread(() -> {
        for (int i = 20; i < 40; i++) {
            A a =listA.get(i);
            synchronized (a){
                if(i==20||i==22){
                    out.println("thread3 第"+ i + "次");
                    out.println((ClassLayout.parseInstance(a).toPrintable()));
                }
            }
        }
    });
    t3.start();


    Thread.sleep(10000);
    out.println("重新输出新实例A");
    out.println((ClassLayout.parseInstance(new A()).toPrintable()));
}
 
来看看输出结果,这部分和上面批量偏向结果的大相径庭。重点关注记录的线程ID信息
前20个对象,并没有触发了批量重偏向机制,线程t2执行释放同步锁后,转变为无锁形态
第20~40个对象,触发了批量重偏向机制,对象为偏向锁状态,偏向线程t2,线程t2的ID信息为540039429
而41个对象之后,也没有触发了批量重偏向机制,对象仍偏向线程t1,线程t1的ID信息为540002309

 

再来看看最后新生成的对象A。值得注意的是:本应该为可偏向状态偏向锁的新对象,在经历过批量重偏向和批量撤销后直接在实例化后转为无锁。

 

简单总结

1、批量重偏向和批量撤销是针对类的优化,和对象无关。
2、偏向锁重偏向一次之后不可再次重偏向。
3、当某个类已经触发批量撤销机制后,JVM会默认当前类产生了严重的问题,剥夺了该类的新实例对象使用偏向

 

 

 

Guess you like

Origin www.cnblogs.com/LemonFive/p/11246120.html