学习资料Java学习笔记多线程

Java 锁 synchronized VS Lock

2016-12-23  本文已影响541人  专职跑龙套

synchronized 关键字

synchronized 的问题:

Lock 接口 VS synchronized 关键字

Lock 接口

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

可重入锁

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

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 的并行读写操作:

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();
        }
    }
}

可中断锁

在上文中已经提到过:

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();
    }
}

公平锁

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

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

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

读写锁

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

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

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

可以看出:

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();
        }
    }
}
上一篇 下一篇

猜你喜欢

热点阅读