类锁,对象锁

  • Post author:
  • Post category:其他


对象锁示例:

@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)



版权声明:本文为YM1137746713原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。