【问题标题】:What is advantage of locks over wait/notify?与等待/通知相比,锁有什么优势?
【发布时间】:2012-03-29 13:48:57
【问题描述】:

与等待/通知相比,锁有什么优势? 代码非常相似。

    private Object full = new Object();
private Object empty = new Object();

private Object data = null;

public static void main(String[] args) {
    Test test = new Test();
    new Thread(test.new Producer()).start();
    new Thread(test.new Consumer()).start();
}

public void push(Object d) {
    synchronized (full) {
        while (data != null)
            try {
                full.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
    }
    data = d;
    System.out.println("push");
    synchronized (empty) {
        if (data != null)
            empty.notify();
    }
}

public Object pop() {
    synchronized (empty) {
        while (data == null)
            try {
                empty.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
    }
    Object o = data;
    data = null;
    System.out.println("pop");
    synchronized (full) {
        if (data == null)
            full.notify();
    }
    return o;
}

class Producer implements Runnable {
    public void run() {
        while (true) {
            push(new Object());
        }
    }
}

class Consumer implements Runnable {
    public void run() {
        while (true) {
            pop();
        }
    }
}

    private final ReentrantLock lock = new ReentrantLock();
private final Condition fullState = lock.newCondition();
private final Condition emptyState = lock.newCondition();

private Object data = null;

public static void main(String[] args) {
    Test test = new Test();
    new Thread(test.new Producer()).start();
    new Thread(test.new Consumer()).start();
}

public void push(Object d) {
    lock.lock();
    try {
        while (data != null)
            try {
                fullState.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        data = d;
        System.out.println("push");
        emptyState.signal();
    } finally {
        lock.unlock();
    }
}

public Object pop() {
    Object result;
    lock.lock();
    try {
        while (data == null)
            try {
                emptyState.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        result = data;
        data = null;
        System.out.println("pop");
        fullState.signal();
    } finally {
        lock.unlock();
    }
    return result;
}

class Producer implements Runnable {
    public void run() {
        while (true) {
            push(new Object());
        }
    }
}

class Consumer implements Runnable {
    public void run() {
        while (true) {
            pop();
        }
    }
}

【问题讨论】:

  • 我猜locks 更难出错...我见过很多有经验的程序员在获得wait/@987654325 的一段代码之前需要查看文档@ 对。

标签: java concurrency


【解决方案1】:

查看 ReeentratLock 的 JavaDoc,您的问题将得到解答。

“一种可重入互斥锁,其基本行为和语义与使用同步方法和语句访问的隐式监控锁相同,但具有扩展功能。”

http://docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/locks/ReentrantLock.html

【讨论】:

    猜你喜欢
    • 2019-07-17
    • 2016-07-04
    • 2011-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-27
    • 1970-01-01
    • 2010-09-24
    相关资源
    最近更新 更多