Java 锁 synchronized VS Lock

作者: 专职跑龙套 | 来源:发表于2016-12-23 11:41 被阅读541次

synchronized 关键字

  • 用在普通方法前 synchronized void func():对象锁

    • 等价与 synchronized(this) {...}
  • 用在 static 方法前 synchronized static void func():类锁

    • 等价与 synchronized(A.class) {...}
  • synchronized(obj) {...}:对象锁

synchronized 的问题:

  • 不区分读写。如果多个线程对同一个对象进行读操作,实际上不应该有冲突,但是 synchronized 会导致线程等待。
  • 会导致线程无限等待。不可以中断等待。

Lock 接口 VS synchronized 关键字

  • 类型:
    • Lock 为接口,有不同的实现类
    • synchronized 为关键字
  • 锁的释放:
    • Lock 需要手动释放锁,即 unLock()
    • synchronized 不需要手动释放锁,synchronized 代码块执行完,自动释放锁
  • 等待的中断:
    • Lock 可以中断等待,即 t.interrupt()
    • synchronized 会导致线程无限等待,不可中断
  • Lock 可以知道线程有没有成功获得锁,即 tryLock(),synchronized不支持

Lock 接口

Java 5 的 concurrent 包开始提供。
提供的方法包括:

  • void lock();:获取锁,如果不能成功获取,则等待,与 synchronized 类似
  • void lockInterruptibly();:获取锁,如果不能成功获取,则等待,且能够响应中断 t.interrupt()
  • boolean tryLock();:获取锁,如果成功,返回 true,如果不成功,返回 false,不等待
  • boolean tryLock(long time, TimeUnit unit);在一定的时间内,获取锁,如果成功,返回 true,如果不成功,返回 false,不等待
  • void unlock();:释放锁

可重入锁

可重入锁:锁基于线程分配,而不是基于方法分配。

synchronized 为可重入锁,例如:
在下面的代码中,在线程进入 f1() 方法时已经获得了对象的锁,因此在 f1() 中调用 f2() 时,不需要重新申请锁。

public synchronized void f1() {
  // 不需要重新申请锁
  f2();
}
public synchronized void f2() {
  // to do
}

ReentrantLock 可重入锁

Java 5 的 concurrent 包开始提供,继承了 Lock 接口。
public class ReentrantLock implements Lock, java.io.Serializable {

关于 ReentrantLock 的实现原理,参见 Java AQS AbstractQueuedSynchronizer

下面的例子中提供了两种方式来控制对共享变量 count 的并行读写操作:

  • incrementAndGetCount():使用 ReentrantLock:
    • 记住:unlock() 操作不能忘记,应该放在 finally 中,确保在出现异常状况下也会被调用。
  • incrementAndGetCountWithSynchronized():使用 synchronized 关键字。
public class Lock_Test {
    private Lock lock = new ReentrantLock();

    private int count = 0;

    private int incrementAndGetCount() {
        lock.lock();

        try {
            System.out.println(Thread.currentThread().getName() + " gets Count: " + count);
            return count++;
        } finally {
            lock.unlock();
        }
    }

    private synchronized int incrementAndGetCountWithSynchronized() {
        System.out.println(Thread.currentThread().getName() + " gets Count: " + count);
        return count++;
    }

    public static void main(String[] args) {
        Lock_Test test = new Lock_Test();

        for (int i = 0; i < 5; i++) {
            new Thread() {
                public void run() {
                    test.incrementAndGetCount();

                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException ex) {
                        ex.printStackTrace();
                    }
                }
            }.start();
        }
    }
}

可中断锁

在上文中已经提到过:

  • synchronized 会导致线程无限等待,不可中断
  • Lock 可以中断等待,即 t.interrupt(),示例如下:
    线程 t1 获得了锁,但是并没有在 finally 中释放锁,因此可以通过 t2.interrupt(); 使得线程 t2 停止等待。
    注意:想要能够响应中断,需使用 lock.lockInterruptibly(); 而不能是 lock.lock();
public class Lock_Test2 {
    private Lock lock = new ReentrantLock();

    private int count = 0;

    private int incrementAndGetCount() {
        try {
            // 想要能够响应中断,需使用 lock.lockInterruptibly(); 而不能是 lock.lock();
            lock.lockInterruptibly();

            System.out.println(Thread.currentThread().getName() + " gets Count: " + count);
            return count++;
        } catch (Exception e) {
            return 0;
        } finally {
            // 并没有在 finally 中释放锁
            // lock.unlock();
        }
    }

    public static void main(String[] args) {
        Lock_Test2 test = new Lock_Test2();

        Thread t1 = new Thread() {
            public void run() {
                test.incrementAndGetCount();

                try {
                    Thread.sleep(100);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
            }
        };

        Thread t2 = new Thread() {
            public void run() {
                test.incrementAndGetCount();

                try {
                    Thread.sleep(100);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
            }
        };

        t1.start();
        t2.start();
        t2.interrupt();
    }
}

公平锁

  • 公平锁:加锁前检查是否有排队等待的线程,先来先得 FIFO,即先排队,再尝试获取锁
  • 非公平锁:加锁时不考虑排队等待问题,直接尝试获取锁,获取不到自动到队尾等待,即先尝试获取锁,再排队

ReentrantLock 默认是非公平锁,可以通过构造方法设置为公平锁:

    public ReentrantLock(boolean fair) {
        sync = fair ? new FairSync() : new NonfairSync();
    }

公平锁可以被用来解决 饥饿 问题。什么是 饥饿 问题?举例说明:

  • T1 锁定资源
  • T2 请求资源,T2 等待
  • T3 请求资源,T3 等待
  • T1 释放资源,T3 获得
  • T4 请求资源,T4 等待
  • T3 释放资源,T4 获得
  • T2 可能永远等待,出现饥饿

读写锁

  • 读锁 被占用,则申请写锁的线程会等待,申请读锁的线程不用等待
  • 写锁 被占用,则申请写锁或读锁的线程都会等待

ReadWriteLock 为读写锁的接口,ReentrantReadWriteLock 为一个实现类。

  • 通过 Lock readLock(); 方法得到读锁
  • 通过 Lock writeLock(); 方法得到写锁

在下面的例子中,输出如下:

Thread-4 is reading count: 0
Thread-0 is reading count: 0
Thread-3 is reading count: 0
Thread-2 is reading count: 0
Thread-1 is reading count: 0
Thread-5 is writing count: 0
Thread-6 is writing count: 1
Thread-7 is writing count: 2
Thread-8 is writing count: 3
Thread-9 is writing count: 4

可以看出:

  • 在某个线程获得读锁后,其他申请读锁的线程不需等待,而其他申请写锁的线程需要等待。因此 writing 都在 reading 之后。
  • 在某个线程获得写锁后,其他申请读锁或者写锁的线程都需要等待。因此 writing 按照 5,6,7,8,9 的顺序依次执行。
public class ReadWriteLock_Test {
    private static final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();

    private static int count = 0;

    public static void readCount() {
        // 申请读锁
        lock.readLock().lock();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
        }
        System.out.println(Thread.currentThread().getName() + " is reading count: " + count);
        // 申请读锁
        lock.readLock().unlock();
    }

    public static void writeCount() {
        // 申请写锁
        lock.writeLock().lock();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
        }
        System.out.println(Thread.currentThread().getName() + " is writing count: " + count++);
        // 释放写锁
        lock.writeLock().unlock();
    }

    public static void main(String[] args) {
        for (int i = 0; i < 5; i++) {
            (new ReadThread()).start();
        }

        for (int i = 0; i < 5; i++) {
            (new WriteThread()).start();
        }
    }

    static class ReadThread extends Thread {
        public void run() {
            readCount();
        }
    }

    static class WriteThread extends Thread {
        public void run() {
            writeCount();
        }
    }
}

相关文章

网友评论

    本文标题:Java 锁 synchronized VS Lock

    本文链接:https://www.haomeiwen.com/subject/loxymttx.html