synchronized

package springcloud.controller;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.locks.ReentrantLock;

public class Jmm07 {

    private static int total = 0;
    private static Object object = new Object();

    private static ReentrantLock lock = new ReentrantLock();//显示锁

    public static void main(String[] args) throws Exception {

        final CountDownLatch countDownLatch = new CountDownLatch(1);
        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                try {
                    countDownLatch.await();//等到其他线程执行完毕
                    for (int j = 0; j < 1000; j++) {
                        synchronized (object) {//内置锁、隐式锁
                            total++;
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }).start();
            Thread.sleep(50);
            countDownLatch.countDown();
            Thread.sleep(200);
        }
        System.out.println(total);
    }
}

synchronized_第1张图片

  1. 在1.6之后,对象锁偏向锁(针对单线程)
  2. 对象锁轻量级锁(针对多线程,但线程竞争不激烈(锁的时间占用端,可以多线程可以交替执行)),内部循环执行,会一直占着cpu(spin)
  3. 占着cpu执行的时间比重新唤醒线程的效率要高
  4. 但是循环的次数是有限制的,比如while设置为100,则锁进行升级(说明竞争激烈,要释放锁)
  5. synchronized 锁的升级过程是不可逆的
  6. ReentrantLock 和synchronized 区别不大

以下是synchronized 锁加的地方

package springcloud.controller;

import java.util.concurrent.locks.ReentrantLock;

public class Jmm08 {
//修饰静态方法
    private static int total = 0;
    private static Object object = new Object();

    private static ReentrantLock lock = new ReentrantLock();//显示锁

    public static void main(String[] args) {
        Jmm08.decrStock();
    }

    static int stock;

    public static synchronized void decrStock() {
        //加锁加在Jmm08这个对象上
        System.out.println(stock);
    }
}
package springcloud.controller;

import java.util.concurrent.locks.ReentrantLock;

public class Jmm09 {
//修饰非静态方法
    private static ReentrantLock lock = new ReentrantLock();//显示锁

    public static void main(String[] args) {
        new Jmm09().decrStock();
    }
    static int stock;
    public synchronized void decrStock() {
        //加锁加在this对象上
        System.out.println(stock);
    }
}

package springcloud.controller;

public class Jmm10 {
    private static Object object = new Object();
//加在方法内部
    public static void main(String[] args) {
        new Jmm10().decrStock();
    }

    static int stock;

    public synchronized void decrStock() {
        //方法内部加同步块
        synchronized (object) {
            if (stock <= 0) {
                System.out.println("库存为0");
            } else {
                System.out.println(stock);
            }
        }

    }
}

内存锁状态记录的工具

synchronized_第2张图片

        <!--内存锁状态打印的工具,记录锁的升级过程-->
        <dependency>
            <groupId>org.openjdk.jol</groupId>
            <artifactId>jol-core</artifactId>
            <version>0.10</version>
        </dependency>
import org.openjdk.jol.info.ClassLayout;

public class T0_ObjeatSize {

    public static void main(String[] args) {
        Object o = new Object();
        System.out.println(ClassLayout.parseInstance(o).toPrintable());
    }
}

得到结果

java.lang.Object object internals:
 OFFSET  SIZE   TYPE DESCRIPTION                               VALUE
      0     4        (object header)                           01 00 00 00 (00000001 00000000 00000000 00000000) (1)
      4     4        (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)
      8     4        (object header)                           e5 01 00 20 (11100101 00000001 00000000 00100000) (536871397)
     12     4        (loss due to the next object alignment)
Instance size: 16 bytes
Space losses: 0 bytes internal + 4 bytes external = 4 bytes total

给予操作系统的大小端问题,查看的结果应该为
01 00 00 00 (00000001 00000000 00000000 00000000) (1)
反过来
00 00 00 01(01代表无锁状态)

开启偏向锁,已经让偏向锁升级成为轻量级锁

public class T0_ObjeatSize {

    public static void main(String[] args) {

        try {
            //TimeUnit.SECONDS.sleep(5);
            //等待5秒开启偏向锁
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Object o = new Object();
        System.out.println(ClassLayout.parseInstance(o).toPrintable());

        new Thread(() -> {
            synchronized (o) {
                System.out.println(ClassLayout.parseInstance(o).toPrintable());
            }
        }).start();

        try {
            //TimeUnit.SECONDS.sleep(5);
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //如果偏向锁没有被重偏向  就会一直记录之前的状态
        System.out.println(ClassLayout.parseInstance(o).toPrintable());

        //多个线程,偏向锁升级成轻量级锁
        new Thread(() -> {
            synchronized (o) {
                System.out.println(ClassLayout.parseInstance(o).toPrintable());
            }
        }).start();
    }
}

你可能感兴趣的:(mysql,数据库)