互斥同步(synchronized、ReentrantLock)

1. 互斥同步

Java 提供了两种锁机制来控制多个线程对共享资源的互斥访问:

  1.  JVM 实现的 synchronized
  2. JDK 实现的 ReentrantLock

1.1 synchronized

详见另一篇博客

1.1.1 同步一个代码块

public void func() {
    synchronized (this) {
        // ...
    }
}

它只作用于同一个对象,如果调用两个对象上的同步代码块,就不会进行同步。

对于以下代码,使用 ExecutorService 执行了两个线程,由于调用的是同一个对象的同步代码块,因此这两个线程会进行同步。

当一个线程进入同步语句块时,另一个线程就必须等待。

public class SynchronizedExample {
    public void func1() {
        synchronized (this) {
            for (int i = 0; i < 10; i++) {
                System.out.print(i + " ");
            }
        }
    }
}

public static void main(String[] args) {
    SynchronizedExample e1 = new SynchronizedExample();
    ExecutorService executorService = Executors.newCachedThreadPool();
    executorService.execute(() -> e1.func1());    
    executorService.execute(() -> e1.func1());
}

运行结果:

对于以下代码,两个线程调用了不同对象的同步代码块,因此这两个线程就不需要同步。

从输出结果可以看出,两个线程交叉执行。

public static void main(String[] args) {
    SynchronizedExample e1 = new SynchronizedExample();
    SynchronizedExample e2 = new SynchronizedExample();
    ExecutorService executorService = Executors.newCachedThreadPool();
    executorService.execute(() -> e1.func1());
    executorService.execute(() -> e2.func1());
}

运行结果:

1.1.2 同步一个方法

public synchronized void func () {
    // ...
}

它和同步代码块一样,作用于同一个对象。

1.1.3 同步一个类

public void func() {
    synchronized (SynchronizedExample.class) {
        // ...
    }
}

作用于整个类,也就是说两个线程调用同一个类的不同对象上的这种同步语句,也会进行同步。

public class SynchronizedExample {
    public void func2() {
        synchronized (SynchronizedExample.class) {
            for (int i = 0; i < 10; i++) {
                System.out.print(i + " ");
            }
        }
    }
}

public static void main(String[] args) {
    SynchronizedExample e1 = new SynchronizedExample();
    SynchronizedExample e2 = new SynchronizedExample();
    ExecutorService executorService = Executors.newCachedThreadPool();
    executorService.execute(() -> e1.func2());
    executorService.execute(() -> e2.func2());
}

运行结果:

1.1.4 同步一个静态方法

public synchronized static void fun() {
    // ...
}

作用于整个类。

1.2  ReentrantLock

ReentrantLock 是 java.util.concurrent(J.U.C) 包中的锁

public class LockExample {
    private Lock lock = new ReentrantLock();
    public void func() {
        lock.lock();
        try {
            for (int i = 0; i < 10; i++) {
                System.out.print(i + " ");
            }
        } finally {
            lock.unlock(); // 确保释放锁,从而避免发生死锁。
        }
    }
}

public static void main(String[] args) {
    LockExample lockExample = new LockExample();
    ExecutorService executorService = Executors.newCachedThreadPool();
    executorService.execute(() -> lockExample.func());
    executorService.execute(() -> lockExample.func());
}

运行结果:

1.3 比较

  synchronized ReentrantLock
锁的实现 JVM 实现的 JDK 实现的
性能

新版本 Java 对 synchronized 进行了很多优化

例如自旋锁等;

与ReentrantLock 大致相同。

 
等待可中断* 不行 可中断
公平锁* 非公平的 也是非公平的,但是
也可以是公平的。
锁绑定多个条件   可以同时绑定多个 Condition 对象

等待可中断:当持有锁的线程长期不释放锁的时候,正在等待的线程可以选择放弃等待,改为处理其他事情。
公平锁:公平锁是指多个线程在等待同一个锁时,必须按照申请锁的时间顺序来依次获得锁。

猜你喜欢

转载自blog.csdn.net/Zhxin606a/article/details/89435495