【Android面试|华为|锁相关】- synchronize(this) 和 synchronize(class)有什么区别

华为面试官问了其中一个问题

Q: synchronize(this) 和 synchronize(class)一样么? 是否可以用synchronize(this) 来锁
A: 当使用 synchronized 加锁 class 时,无论共享一个对象还是创建多个对象,它们用的都是同一把锁,而使用 synchronized 加锁 this 时,只有同一个对象会使用同一把锁,不同对象之间的锁是不同的。

synchronized 用法
synchronized 可以用来修饰普通方法、静态方法和代码块。 ​

  1. 修饰普通方法
/**
 * synchronized 修饰普通方法
 */
public synchronized void method() {
   
    // .......
} 

当 synchronized 修饰普通方法时,被修饰的方法被称为同步方法,其作用范围是整个方法,作用的对象是调用这个方法的对象。 ​
2. 修饰静态方法

/**
 * synchronized 修饰静态方法
 */
public static synchronized void staticMethod() {
   
    // .......
} 

当 synchronized 修饰静态的方法时,其作用的范围是整个方法,作用对象是调用这个类的所有对象。

  1. 修饰代码块
    为了减少锁的粒度,我们可以选择在一个方法中的某个部分使用 synchronized 来修饰(一段代码块),从而实现对一个方法中的部分代码进行加锁,实现代码如下:
public void classMethod() throws InterruptedException {
   
    // 前置代码...
    
    // 加锁代码
    synchronized (SynchronizedExample.class) {
   
        // ......
    }
    
    // 后置代码...
} 

以上代码在执行时,被修饰的代码块称为同步语句块,其作用范围是大括号“{}”括起来的代码块,作用的对象是调用这个代码块的对象。
但以上代码,除了可以加锁 class 之外,还可以加锁 this,具体示例如下:

public void classMethod() throws InterruptedException {
   
    // 前置处理代码...
    synchronized (this) {
   
        // ......
    }
    // 后置处理代码...
} 

那问题来了,使用 synchronized 加锁 this 和 class 的区别是什么?不都是加锁同一个类吗? ​
答案还真不是,加锁 this 和 class 区别还是很大的。下面我们通过以下 4 个示例,来看二者之间的区别。

区别:

  1. 加锁 class 共享一个类实例
    首先,我们创建 5 个线程,调用同一个对象下 synchronized 加锁的 class 代码,具体示例如下:
import java.util.Date;
import java.util.concurrent.TimeUnit;

public class SynchronizedExample {
   

    public static void main(String[] args) {
   
        // 创建当前类实例
        final SynchronizedExample example = new SynchronizedExample();
        // 创建 5 个线程执行任务
        for (int i = 0; i < 5; i++) {
   
            new Thread(new Runnable() {
   
                @Override
                public void run() {
   
                    try {
   
                        // 调用 synchronized 修饰的 class 方法
                        example.classMethod();
                    } catch (InterruptedException e) {
   
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    }

    /**
     * synchronized 修饰的 class 方法
     * @throws InterruptedException
     */
    public void classMethod() throws InterruptedException {
   
        synchronized (SynchronizedExample.class) {
   
            System.out.println(String.format("当前执行线程:%s,执行时间:%s",
                    Thread.currentThread().getName(), new Date()));
            TimeUnit.SECONDS.sleep(1);
        }
    }
} 

从上述结果可以看出,这 5 个线程共享的是同一把锁。

  1. 加锁 class 创建多个实例
    接下来,我们创建 5 个线程,调用不同对象下 synchronized 加锁的 class 代码,具体示例如下:
import java.util.Date;
import java.util.concurrent.TimeUnit;

public class SynchronizedExample {
   

    public static void main(String[] args) {
   
        // 创建 5 个线程执行任务
        for (int i = 0; i < 5; i++) {
   
            new Thread(new Runnable() {
   
                @Override
                public void run() {
   
                    try {
   
                        // 创建类实例
                        SynchronizedExample example = new SynchronizedExample();
                        // 调用 synchronized 修饰的 class 方法
                        example.classMethod();
                    } catch (InterruptedException e) {
   
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    }
    
    /**
     * synchronized 修饰的 class 方法
     * @throws InterruptedException
     */
    public void classMethod() throws InterruptedException {
   
        synchronized (SynchronizedExample.class) {
   
            System.out.println(String.format("当前执行线程:%s,执行时间:%s",
                    Thread.currentThread().getName(), new Date()));
            TimeUnit.SECONDS.sleep(1);
        }
    }
} 

从上述结果可以看出,虽然是不同的对象,但它们使用的仍然是同一把锁。
3. 加锁 this 共享一个类实例
接下来,我们创建 5 个线程,调用 synchronized 加锁 this 的示例。首先我们这 5 个线程调用同一个对象的加锁方法,示例代码如下:

import java.util.Date;
import java.util.concurrent.TimeUnit;

public class SynchronizedExample {
   

    public static void main(String[] args) {
   
        // 创建当前类实例
        final SynchronizedExample example = new SynchronizedExample();
        // 创建 5 个线程执行任务
        for (int i = 0; i < 5; i++) {
   
            new Thread(new Runnable() {
   
                @Override
                public void run() {
   
                    try {
   
                        // 调用 synchronized 修饰的 this 方法
                        example.thisMethod();
                    } catch (InterruptedException e) {
   
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    }
    
    /**
     * synchronized 修饰的 this 方法
     * @throws InterruptedException
     */
    public void thisMethod() throws InterruptedException {
   
        synchronized (this) {
   
            System.out.println(String.format("当前执行线程:%s,执行时间:%s",
                    Thread.currentThread().getName(), new Date()));
            TimeUnit.SECONDS.sleep(1);
        }
    }
} 

从上述结果可以看出,以上线程使用的都是同一把锁。
4. 加锁 this 创建多个类实例
最后一个示例最为特殊,我们使用 synchronized 加锁 this,让这 5 个线程调用各自创建对象的方法,具体示例如下:

import java.util.Date;
import java.util.concurrent.TimeUnit;

public class SynchronizedExample {
   

    public static void main(String[] args) {
   
        // 创建 5 个线程执行任务
        for (int i = 0; i < 5; i++) {
   
            new Thread(new Runnable() {
   
                @Override
                public void run() {
   
                    try {
   
                        // 创建(多个)类实例
                        SynchronizedExample example = new SynchronizedExample();
                        // 调用 synchronized 修饰的 this 方法
                        example.thisMethod();
                    } catch (InterruptedException e) {
   
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    }
    
    /**
     * synchronized 修饰的 this 方法
     * @throws InterruptedException
     */
    public void thisMethod() throws InterruptedException {
   
        synchronized (this) {
   
            System.out.println(String.format("当前执行线程:%s,执行时间:%s",
                    Thread.currentThread().getName(), new Date()));
            TimeUnit.SECONDS.sleep(1);
        }
    }
} 

从上述结果可以看出,当使用 synchronized 加锁 this 时,如果线程调用的不是同一个对象,那么这些线程之间使用的锁都是自己独立的锁,这个结果就和 synchronized 加锁 class 的结果完全不同了。

相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-08 17:40:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-08 17:40:02       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-08 17:40:02       82 阅读
  4. Python语言-面向对象

    2023-12-08 17:40:02       91 阅读

热门阅读

  1. Fiddler抓包测试

    2023-12-08 17:40:02       60 阅读
  2. Vue+ElementUI实现输入框日期框下拉框动态展示

    2023-12-08 17:40:02       60 阅读
  3. 常用的git版本控制有哪些工具或网站呢?

    2023-12-08 17:40:02       73 阅读
  4. Git 还原文件修改

    2023-12-08 17:40:02       59 阅读
  5. 求int型正整数在内存中存储时1的个数

    2023-12-08 17:40:02       49 阅读
  6. 程序员学习方法

    2023-12-08 17:40:02       57 阅读
  7. flask之文件上传

    2023-12-08 17:40:02       63 阅读
  8. JDK、JRE、JVM、SE、EE、ME的区别

    2023-12-08 17:40:02       46 阅读