类锁,对象锁

对象锁示例:

@SpringBootTest
class ForDesignApplicationTests {

    public static void main(String[] args) {
        SyncTest syncTest = new SyncTest();
        Thread thread1 = new Thread(() -> syncTest.test1(), "thread-1");
        Thread thread2 = new Thread(() -> syncTest.test2(), "thread-2");
        thread1.start();
        thread2.start();
    }

    private static class SyncTest {

        @SneakyThrows
        public synchronized void test1() {
            int i = 0;
            while (true) {
                i++;
                System.out.println(Thread.currentThread().getName() + ": " + i);
                Thread.sleep(500);
                if (i == 5) {
                    break;
                }
            }
        }

        @SneakyThrows
        public void test2() {
            synchronized (this) {
                int i = 0;
                while (true) {
                    i++;
                    System.out.println(Thread.currentThread().getName() + ": " + i);
                    Thread.sleep(500);
                    if (i == 5) {
                        break;
                    }
                }
            }
        }

    }

}

输出结果:

thread-1: 1
thread-1: 2
thread-1: 3
thread-1: 4
thread-1: 5
thread-2: 1
thread-2: 2
thread-2: 3
thread-2: 4
thread-2: 5

当我们将test2方法的锁换成另一个对象时

@SpringBootTest
class ForDesignApplicationTests {

    public static void main(String[] args) {
        SyncTest syncTest = new SyncTest();
        Thread thread1 = new Thread(() -> syncTest.test1(), "thread-1");
        Thread thread2 = new Thread(() -> syncTest.test2(), "thread-2");
        thread1.start();
        thread2.start();
    }

    private static class SyncTest {

        Object object = new Object();
        @SneakyThrows
        public synchronized void test1() {
            int i = 0;
            while (true) {
                i++;
                System.out.println(Thread.currentThread().getName() + ": " + i);
                Thread.sleep(500);
                if (i == 5) {
                    break;
                }
            }
        }

        @SneakyThrows
        public void test2() {
            synchronized (object) {
                int i = 0;
                while (true) {
                    i++;
                    System.out.println(Thread.currentThread().getName() + ": " + i);
                    Thread.sleep(500);
                    if (i == 5) {
                        break;
                    }
                }
            }
        }

    }

}

输出结果:

thread-1: 1
thread-2: 1
thread-1: 2
thread-2: 2
thread-2: 3
thread-1: 3
thread-1: 4
thread-2: 4
thread-1: 5
thread-2: 5

结论:不同的对象锁,两者之间不会相互影响,当两个线程同时获取同一把对象锁时,谁先获取到锁,谁就先执行。

类锁示例:

@SpringBootTest
class ForDesignApplicationTests {

    public static void main(String[] args) {
        SyncTest syncTest = new SyncTest();
        Thread thread1 = new Thread(() -> SyncTest.test1(), "thread-1");
        Thread thread2 = new Thread(() -> syncTest.test2(), "thread-2");
        thread1.start();
        thread2.start();
    }

    private static class SyncTest {

        @SneakyThrows
        public static synchronized void test1() {
            int i = 0;
            while (true) {
                i++;
                System.out.println(Thread.currentThread().getName() + ": " + i);
                Thread.sleep(500);
                if (i == 5) {
                    break;
                }
            }
        }

        @SneakyThrows
        public void test2() {
            synchronized (SyncTest.class) {
                int i = 0;
                while (true) {
                    i++;
                    System.out.println(Thread.currentThread().getName() + ": " + i);
                    Thread.sleep(500);
                    if (i == 5) {
                        break;
                    }
                }
            }
        }

    }

}

输出结果:

thread-1: 1
thread-1: 2
thread-1: 3
thread-1: 4
thread-1: 5
thread-2: 1
thread-2: 2
thread-2: 3
thread-2: 4
thread-2: 5

当我们将test2中的类锁换成其他类之后

@SpringBootTest
class ForDesignApplicationTests {

    public static void main(String[] args) {
        SyncTest syncTest = new SyncTest();
        Thread thread1 = new Thread(() -> SyncTest.test1(), "thread-1");
        Thread thread2 = new Thread(() -> syncTest.test2(), "thread-2");
        thread1.start();
        thread2.start();
    }

    private static class SyncTest {

        @SneakyThrows
        public static synchronized void test1() {
            int i = 0;
            while (true) {
                i++;
                System.out.println(Thread.currentThread().getName() + ": " + i);
                Thread.sleep(500);
                if (i == 5) {
                    break;
                }
            }
        }

        @SneakyThrows
        public void test2() {
            synchronized (ForDesignApplicationTests.class) {
                int i = 0;
                while (true) {
                    i++;
                    System.out.println(Thread.currentThread().getName() + ": " + i);
                    Thread.sleep(500);
                    if (i == 5) {
                        break;
                    }
                }
            }
        }

    }

}

输出:

thread-1: 1
thread-2: 1
thread-1: 2
thread-2: 2
thread-1: 3
thread-2: 3
thread-1: 4
thread-2: 4
thread-1: 5
thread-2: 5
 

总结:

当synchronized作用于静态方法上时相当于类锁,而作用于非静态方法上时相当于对象锁;

对象锁是用于对象实例方法,或者一个对象实例上的,类锁是用于类的静态方法或者一个类的class对象上的我们知道,类的对象实例可以有很多个,但是每个类只有一个class对象,所以不同对象实例的对象锁是互不干扰的,但是每个类只有一个类锁。但是有一点必须注意的是,其实类锁只是一个概念上的东西,并不是真实存在的,它只是用来帮助我们理解锁定实例方法和静态方法的区别的

参考:Java锁Synchronized对象锁和类锁区别 - 森林木马 - 博客园 (cnblogs.com)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值