文档

Java™ 教程-Java Tutorials 中文版
锁对象
Trail: Essential Classes
Lesson: Concurrency
Section: High Level Concurrency Objects

锁对象

同步代码依赖于简单的可重入锁。这种锁易于使用,但有许多限制。java.util.concurrent.locks 包支持更复杂的锁惯用语法。我们不会详细检查此包,而是将重点放在其最基本的接口 Lock 上。

Lock 对象与同步代码使用的内部锁非常相似。与内部锁一样,一次只有一个线程可以拥有 Lock 对象。Lock 对象还通过其关联的 Condition 对象支持 wait/notify 机制。

Lock 对象优于内部锁的最大优点是它们能够退出获取锁定的尝试。如果锁不立即可用或请求锁超时(如果指定超时时间),则 tryLock 方法退出。如果另一个线程在获取锁之前发送中断,则 lockInterruptibly 方法会退出。

让我们使用 Lock 对象来解决我们在 Liveness 中看到的死锁问题。Alphonse 和 Gaston 已经把自己训练成能注意到朋友何时要鞠躬。我们通过要求我们的 Friend 对象必须在继续执行鞠躬之前获取 both (两个) 参与者的锁来模拟这种改进。以下是改进模型的源代码,Safelock。为了证明这个惯用语法的多功能性,我们假设 Alphonse 和 Gaston 如此迷恋他们新发现的安全鞠躬能力,他们不能停止相互鞠躬:


import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.Random;

public class Safelock {
    static class Friend {
        private final String name;
        private final Lock lock = new ReentrantLock();

        public Friend(String name) {
            this.name = name;
        }

        public String getName() {
            return this.name;
        }

        public boolean impendingBow(Friend bower) {
            Boolean myLock = false;
            Boolean yourLock = false;
            try {
                myLock = lock.tryLock();
                yourLock = bower.lock.tryLock();
            } finally {
                if (! (myLock && yourLock)) {
                    if (myLock) {
                        lock.unlock();
                    }
                    if (yourLock) {
                        bower.lock.unlock();
                    }
                }
            }
            return myLock && yourLock;
        }
            
        public void bow(Friend bower) {
            if (impendingBow(bower)) {
                try {
                    System.out.format("%s: %s has"
                        + " bowed to me!%n", 
                        this.name, bower.getName());
                    bower.bowBack(this);
                } finally {
                    lock.unlock();
                    bower.lock.unlock();
                }
            } else {
                System.out.format("%s: %s started"
                    + " to bow to me, but saw that"
                    + " I was already bowing to"
                    + " him.%n",
                    this.name, bower.getName());
            }
        }

        public void bowBack(Friend bower) {
            System.out.format("%s: %s has" +
                " bowed back to me!%n",
                this.name, bower.getName());
        }
    }

    static class BowLoop implements Runnable {
        private Friend bower;
        private Friend bowee;

        public BowLoop(Friend bower, Friend bowee) {
            this.bower = bower;
            this.bowee = bowee;
        }
    
        public void run() {
            Random random = new Random();
            for (;;) {
                try {
                    Thread.sleep(random.nextInt(10));
                } catch (InterruptedException e) {}
                bowee.bow(bower);
            }
        }
    }
            

    public static void main(String[] args) {
        final Friend alphonse =
            new Friend("Alphonse");
        final Friend gaston =
            new Friend("Gaston");
        new Thread(new BowLoop(alphonse, gaston)).start();
        new Thread(new BowLoop(gaston, alphonse)).start();
    }
}

Previous page: High Level Concurrency Objects
Next page: Executors