互斥同步(synchronized、Lock、ReentrantLock、ReadWriteLock、ReentrantReadWriteLock)
互斥同步
Java 提供了两种锁机制来控制多个线程对共享资源的互斥访问:
1. synchronized
synchronized关键字保证在同一时刻,只有一个线程可以执行某个对象内某一个方法或某一段代码块。
重量级锁。包含两个特征:互斥性和可见性。
synchronized可以解决一个线程看到对象处于不一致的状态,可以保证进入同步方法或者同步代码块的每个线程都可以看到由同一个锁保护之前所有的修改效果。
实现同步的基础:Java中每个对象都可作为锁。
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.2 同步一个方法
public synchronized void func () {// ... }它和同步代码块一样,作用于同一个对象。
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.4 同步一个静态方法
public synchronized static void fun() {// ... }作用于整个类。
1.5 synchronized缺陷:
- 不可中断:A执行,等待。当A阻塞时,B得一直等待
- 如果多个线程都只是进行读操作,当一个线程在进行读操作时,其他线程只能等待无法进行读操作。(一般上希望读锁是共享锁,而写锁是排它锁)
- 无法知道线程有没有成功获取到锁
2. java.util.concurrent.locks包中常用的类和接口
2.1 Lock
Lock是一个接口:
public interface Lock {void lock();void lockInterruptibly() throws InterruptedException;boolean tryLock();boolean tryLock(long time, TimeUnit unit) throws InterruptedException;void unlock();Condition newCondition(); }采用Lock,必须主动去释放锁,并且在发生异常时,不会自动释放锁。
因此一般来说,使用Lock必须在try{}catch{}块中进行,并且将释放锁的操作放在finally块中进行,以保证锁一定被被释放,防止死锁的发生。
2.1.1 获取锁
lock()方法
是平常使用得最多的用来获取锁的一个方法。如果锁已被其他线程获取,则进行等待。
Lock lock = ...; lock.lock(); try{... //处理任务 }catch(Exception ex){... }finally{lock.unlock(); //释放锁 }tryLock()方法
有返回值的,它表示用来尝试获取锁,如果获取成功,则返回true,如果获取失败(即锁已被其他线程获取),则返回false,也就说这个方法无论如何都会立即返回。在拿不到锁时不会一直在那等待。
Lock lock = ...; if(lock.tryLock()) {try{...//处理任务}catch(Exception ex){...}finally{lock.unlock(); //释放锁} }else {...//如果不能获取锁,则直接做其他事情 }lockInterruptibly()方法
比较特殊,当通过这个方法去获取锁时,如果线程正在等待获取锁,则这个线程能够响应中断,即中断线程的等待状态。
也就使说,当两个线程同时通过lock.lockInterruptibly()想获取某个锁时,假若此时线程A获取到了锁,而线程B只有在等待,那么对线程B调用threadB.interrupt()方法能够中断线程B的等待过程。
由于lockInterruptibly()的声明中抛出了异常,所以lock.lockInterruptibly()必须放在try块中或者在调用lockInterruptibly()的方法外声明抛出InterruptedException。
public void method() throws InterruptedException {lock.lockInterruptibly();try { //.....}finally {lock.unlock();} }注意,当一个线程获取了锁之后,是不会被interrupt()方法中断的。
因为单独调用interrupt()方法不能中断正在运行过程中的线程,只能中断阻塞过程中的线程。
因此当通过lockInterruptibly()方法获取某个锁时,如果不能获取到,只有进行等待的情况下,是可以响应中断的。
而用synchronized修饰的话,当一个线程处于等待某个锁的状态,是无法被中断的,只有一直等待下去。
2.2 ReentrantLock
ReentrantLock 是 java.util.concurrent(J.U.C) 包中的锁。
唯一实现了Lock接口的类,并且ReentrantLock提供了更多的方法。
2.2.1 lock()
public class Test {private ArrayList<Integer> arrayList = new ArrayList<Integer>();public static void main(String[] args) {final Test test = new Test(); new Thread(){public void run() {test.insert(Thread.currentThread());};}.start();new Thread(){public void run() {test.insert(Thread.currentThread());};}.start();} public void insert(Thread thread) {Lock lock = new ReentrantLock(); //注意这个地方lock.lock();try {System.out.println(thread.getName()+"得到了锁");for(int i=0;i<5;i++) {arrayList.add(i);}} catch (Exception e) {// TODO: handle exception}finally {System.out.println(thread.getName()+"释放了锁");lock.unlock();}} }运行结果:
Thread-0得到了锁
Thread-1得到了锁
Thread-0释放了锁
Thread-1释放了锁
为什么第二个线程怎么会在第一个线程释放锁之前得到了锁?
原因:在insert方法中的lock变量是局部变量,每个线程执行该方法时都会保存一个副本,那么理所当然每个线程执行到lock.lock()处获取的是不同的锁,所以就不会发生冲突。
知道了原因改起来就比较容易了,只需要将lock声明为类的属性即可。
import java.util.ArrayList; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock;public class Test{Lock lock = new ReentrantLock(); //注意这个地方private ArrayList<Integer> arrayList = new ArrayList<Integer>();public static void main(String[] args) {final Testtest = new Test(); new Thread(){public void run() {test.insert(Thread.currentThread());};}.start();new Thread(){public void run() {test.insert(Thread.currentThread());};}.start();} public void insert(Thread thread) {lock.lock();try {System.out.println(thread.getName()+"得到了锁");for(int i=0;i<5;i++) {arrayList.add(i);}} catch (Exception e) {// TODO: handle exception}finally {System.out.println(thread.getName()+"释放了锁");lock.unlock();}} }2.2.2 tryLock()
package Test1;import java.util.ArrayList; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock;public class Code1 {Lock lock = new ReentrantLock(); //注意这个地方private ArrayList<Integer> arrayList = new ArrayList<Integer>();public static void main(String[] args) {final Code1 test = new Code1(); new Thread(){public void run() {test.insert(Thread.currentThread());};}.start();new Thread(){public void run() {test.insert(Thread.currentThread());};}.start();} public void insert(Thread thread) {if(lock.tryLock()) {try {System.out.println(thread.getName()+"得到了锁");for(int i=0;i<5;i++) {arrayList.add(i);}} catch (Exception e) {// TODO: handle exception}finally {System.out.println(thread.getName()+"释放了锁");lock.unlock();}}else {System.out.println(thread.getName()+"未获取到锁");}} }运行结果:
Thread-0得到了锁
Thread-0释放了锁
Thread-1未获取到锁
2.2.3 lockInterruptibly()
package Test1;import java.util.concurrent.locks.*;public class Code1 implements Runnable{Lock lock = new ReentrantLock();public static void main(String[] args) throws InterruptedException {Code1 test = new Code1();//线程1Thread thread1 = new Thread(test);thread1.start();//线程2Thread thread2 = new Thread(test);thread2.start();//让主线程等待两秒,让线程2尝试获取线程1中的锁,两秒后尝试中断线程2Thread.sleep(2000);thread2.interrupt();}public void run(){try {insert();} catch (InterruptedException e) {System.out.println(Thread.currentThread().getName()+"被中断");e.printStackTrace();}} public void insert() throws InterruptedException {lock.lockInterruptibly();/* 要想得到正确的中断等待线程,* 获取锁必须放在trycatch语句块外* 然后抛出InterruptedException*/System.out.println(Thread.currentThread().getName() + "获取了锁");try {for (;;) {}} finally {lock.unlock();System.out.println(Thread.currentThread().getName() + "释放了锁");}} }
运行结果:
Thread-0获取了锁
Thread-1被中断
java.lang.InterruptedException...
运行结果:
2.3 ReadWriteLock
ReadWriteLock也是一个接口,在它里面只定义了两个方法:
public interface ReadWriteLock {/*** Returns the lock used for reading.** @return the lock used for reading.*/Lock readLock();/*** Returns the lock used for writing.** @return the lock used for writing.*/Lock writeLock(); }一个用来获取读锁,一个用来获取写锁。
也就是说将文件的读写操作分开,分成2个锁来分配给线程,从而使得多个线程可以同时进行读操作。
下面的ReentrantReadWriteLock实现了ReadWriteLock接口。
2.4 ReentrantReadWriteLock
里面提供了很多丰富的方法,不过最主要的有两个方法:readLock()和writeLock()用来获取读锁和写锁。
2.4.1 假如有多个线程要同时进行读操作的话,先看一下synchronized达到的效果:
package Test1;import java.util.concurrent.locks.ReentrantReadWriteLock;public class Code1 {private ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();public static void main(String[] args) {final Code1 test = new Code1();new Thread() {public void run() {test.get(Thread.currentThread());};}.start();new Thread() {public void run() {test.get(Thread.currentThread());};}.start();}public synchronized void get(Thread thread) {long start = System.currentTimeMillis();while (System.currentTimeMillis() - start <= 1) {System.out.println(thread.getName() + "正在进行读操作");}System.out.println(thread.getName() + "读操作完毕");} }运行结果:
Thread-0正在进行读操作
Thread-0正在进行读操作
Thread-0正在进行读操作
Thread-0正在进行读操作
Thread-0正在进行读操作
Thread-0正在进行读操作
Thread-0正在进行读操作
Thread-0正在进行读操作
Thread-0读操作完毕
Thread-1正在进行读操作
Thread-1正在进行读操作
Thread-1正在进行读操作
Thread-1正在进行读操作
Thread-1正在进行读操作
Thread-1正在进行读操作
Thread-1正在进行读操作
Thread-1正在进行读操作
Thread-1正在进行读操作
Thread-1正在进行读操作
Thread-1读操作完毕
直到thread1执行完读操作之后,才会打印thread2执行读操作的信息。
2.4.2 改成用读写锁:
package Test1;import java.util.concurrent.locks.ReentrantReadWriteLock;public class Code1 {private ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();public static void main(String[] args) {final Code1 test = new Code1();new Thread() {public void run() {test.get(Thread.currentThread());};}.start();new Thread() {public void run() {test.get(Thread.currentThread());};}.start();}//更改此处public void get(Thread thread) {rwl.readLock().lock();try {long start = System.currentTimeMillis();while (System.currentTimeMillis() - start <= 1) {System.out.println(thread.getName() + "正在进行读操作");}System.out.println(thread.getName() + "读操作完毕");} finally {rwl.readLock().unlock();}} }运行结果:
Thread-0正在进行读操作
Thread-0正在进行读操作
Thread-0正在进行读操作
Thread-0正在进行读操作
Thread-1正在进行读操作
Thread-1正在进行读操作
Thread-1正在进行读操作
Thread-1正在进行读操作
Thread-1正在进行读操作
Thread-1正在进行读操作
Thread-1读操作完毕
Thread-0读操作完毕
thread1和thread2在同时进行读操作,可大大提升了读操作的效率。
如果有一个线程已经占用了读锁,则此时其他线程如果要申请写锁,则申请写锁的线程会一直等待释放读锁。
如果有一个线程已经占用了写锁,则此时其他线程如果申请写锁或者读锁,则申请的线程会一直等待释放写锁。
3. 比较
| JVM 实现的 | JDK 实现的 |
| 自动释放 | 手动finally块中,unlock() |
| 无法判断是否获取锁的状态, | |
| 新版本 Java 对 synchronized 进行了很多优化 例如自旋锁等; 与ReentrantLock 大致相同。 | |
| 不行 | 可中断 |
| 非公平的 | 也是非公平的,但是 也可以是公平的。 |
| 可以同时绑定多个 Condition 对象 | |
| 适合代码少量的同步问题 | 大量同步的代码的同步问题 |
等待可中断:当持有锁的线程长期不释放锁的时候,正在等待的线程可以选择放弃等待,改为处理其他事情。
公平锁:公平锁是指多个线程在等待同一个锁时,必须按照申请锁的时间顺序来依次获得锁。
参考:https://blog.csdn.net/qq_19734597/article/details/80874972
总结
以上是生活随笔为你收集整理的互斥同步(synchronized、Lock、ReentrantLock、ReadWriteLock、ReentrantReadWriteLock)的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 中断(interrupted()、isI
- 下一篇: AQS(CountdownLatch、C