java线程锁

java线程锁是重入锁:一个线程可以重复获取锁
package com.example.demo.config;

public class Thread1 extends Thread{
    public void run() {
        System.out.println("Thread-1: try get lock A...");
        synchronized (Demo.lockA) { //给A加锁
            System.out.println("Thread-1: lock A got.");
            Demo.sleep1s();
            System.out.println("Thread-1: try get lock B...");
            synchronized (Demo.lockB) {//获取B锁
                System.out.println("Thread-1: lock B got.");
                Demo.sleep1s();
            }
            System.out.println("Thread-1: lock B released.");
        }
        System.out.println("Thread-1: lock A released.");
    }
}
package com.example.demo.config;

public class Thread2 extends Thread{
    public void run() {
        System.out.println("Thread-2: try get lock B...");
        synchronized (Demo.lockB) {//给B加锁
            System.out.println("Thread-2: lock B got.");
            Demo.sleep1s();
            System.out.println("Thread-2: try get lock A...");
            synchronized (Demo.lockA) {//获取A锁
                System.out.println("Thread-2: lock A got.");
                Demo.sleep1s();
            }
            System.out.println("Thread-2: lock A released.");
        }
        System.out.println("Thread-2: lock B released.");
    }
}
package com.example.demo.config;


public class Demo {
    static final Object lockA = new Object();
    static final Object lockB = new Object();
    private Integer value=0;
    private Integer another=0;

    public Integer getValue() {
        return value;
    }

    public void setValue(Integer value) {
        this.value = value;
    }

    public Integer getAnother() {
        return another;
    }

    public void setAnother(Integer another) {
        this.another = another;
    }

    public void add(int m) {
        synchronized (lockA) { // 获得lockA的锁
            System.out.println("获得lockA的锁0");
            this.value += m;
            synchronized (lockB) { // 获得lockB的锁
                System.out.println("获得lockB的锁0");
                this.another += m;
            } // 释放lockB的锁
        } // 释放lockA的锁
    }

    public void dec(int m) {
        synchronized (lockB) { // 获得lockB的锁
            System.out.println("获得lockB的锁1");
            this.another -= m;
            synchronized (lockA) { // 获得lockA的锁
                System.out.println("获得lockA的锁1");

                this.value -= m;
            } // 释放lockA的锁
        } // 释放lockB的锁
    }

    static void sleep1s() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args) {
                 new  Thread1().start();
                 new  Thread2().start();

        Demo demo=new Demo();
        try {
            demo.add(2);
            demo.dec(2);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


}
结果:
Connected to the target VM, address: '127.0.0.1:51315', transport: 'socket'
Thread-1: try get lock A...
Thread-2: try get lock B...
Thread-2: lock B got.
Thread-1: lock A got.
Thread-1: try get lock B...
Thread-2: try get lock A...
造成死锁,
线程1 A加锁后获取B
线程2 B加锁后获取A
造成线程1获取B锁进入等待
线程2获取A锁进入等待,死循环下取
思维没有好的解决办法,只有停止虚拟机进行解决
一点点学习,一丝丝进步。不懈怠,才不会被时代淘汰
原文地址:https://www.cnblogs.com/wangbiaohistory/p/15206248.html