synchronized 关键字

synchronized 作用

  能够保证在同一时刻最多只有一个线程执行该段代码,以达到并发安全的效果

synchronized的2种用法

对象锁:包括方法锁(默认锁对象为this当前实例对象)和同步代码块锁(自己指定锁对象)

代码块形式:

/**
 * @Description: 对象锁之一,同步代码块锁
 */
public class Demo1 implements Runnable {
    // 实例
    static Demo1 instance = new Demo1();
    // 自定义对象锁
    Object lock1 = new Object();
    Object lock2 = new Object();

    public void run() {
        synchronized (lock1) {
            System.out.println("对象锁的同步代码块,锁lock1,线程名称:"
                    + Thread.currentThread().getName());
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("lock1,"+Thread.currentThread().getName() + "运行结束!");
        }
        synchronized (lock2) {
            System.out.println("对象锁的同步代码块,锁lock2,线程名称:"
                    + Thread.currentThread().getName());
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("lock2,"+Thread.currentThread().getName() + "运行结束!");
        }
    }

    public static void main(String[] args) {
        Thread t1 = new Thread(instance);
        Thread t2 = new Thread(instance);
        t1.start();
        t2.start();
        while (t1.isAlive() || t2.isAlive()) {
        }
        System.out.println("finished");
    }
}

运行结果如下:

方法锁形式:

/**
 * @Description: 对象锁之二,方法锁
 */
public class Demo2 implements Runnable{
    static  Demo2 instance = new Demo2();

    public void run() {
        method();
    }

    public synchronized  void method(){
        System.out.println("对象锁的方法修饰符形式,名称:"
                +Thread.currentThread().getName()
        );
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + "运行结束!");
    }

    public static void main(String[] args){
        Thread t1 = new Thread(instance);
        Thread t2 = new Thread(instance);
        t1.start();
        t2.start();
        while (t1.isAlive() || t2.isAlive()){

        }
        System.out.println(
                "finished!"
        );
    }
}
运行结果如下:

类锁:指synchronized修饰静态的方法或指定锁为class对象

1.只有一个class对象:java类可能会有很多个对象,但是只有1个class对象

  2.本质:所以所谓类锁,不过是class对象的锁而已

  3.用法和效果:类锁只能在同一时刻被一个对象所拥有

静态方法形式:

/**
 * @Description: 类锁之一,static形式
 */
public class Demo3 implements Runnable{
    static  Demo3 instance1 = new Demo3();
    static  Demo3 instance2 = new Demo3();
    public void run() {
        method();
    }

    public static synchronized  void method(){
        System.out.println("类锁,名称:"
                +Thread.currentThread().getName()
        );
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + "运行结束!");
    }

    public static void main(String[] args){
        Thread t1 = new Thread(instance1);
        Thread t2 = new Thread(instance2);
        t1.start();
        t2.start();
        while (t1.isAlive() || t2.isAlive()){}
        System.out.println("finished!");
    }
}
运行结果如下:

 *.class形式:

/**
 * @Description: 类锁之二:synchronized(.*)形式
 */
public class Demo4 implements Runnable {
    static Demo4 instance1 = new Demo4();
    static Demo4 instance2 = new Demo4();
    public void run() {
       method();
    }

    private void method(){
        synchronized (Demo4.class) {
            System.out.println("类锁,synchronized(.*)形式,名称:"
                    + Thread.currentThread().getName()
            );
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "运行结束!");
        }
    }

    public static void main(String[] args){
        Thread t1 = new Thread(instance1);
        Thread t2 = new Thread(instance2);
        t1.start();
        t2.start();
        while (t1.isAlive() || t2.isAlive()){}
        System.err.println("finished!");
    }
}
运行结果如下:
原文地址:https://www.cnblogs.com/willpan-z/p/10677394.html