两个同步方法(相同对象)在Java中如何同时运行?

huangapple go评论61阅读模式
英文:

How can two sync Methods (same Object) in Java run at the same time?

问题

我在互联网上搜索了一个关于wait()notify()的例子,找到了以下示例。在以下代码块中,getValue()setValue方法都是同步的。如果我创建两个线程,并且第一个线程获得锁,那么第二个线程是否会无限等待,直到第一个线程释放锁?所以getValue()setValue()是否不可能同时运行?如果是的话,wait()notify()方法就没有意义。

tl;dr:如何使getValue()setValue()方法能够在不同线程同时调用?

public class Data {
    private int value;
    private boolean available = false;

    public synchronized int getValue() {
        while (!available) {
            try {
                wait();
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }
        available = false;
        notifyAll();
        return value;
    }

    public synchronized void setValue(int value) {
        while (available) {
            try {
                wait();
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }
        this.value = value;
        available = true;
        notifyAll();
    }
}
英文:

I was searching for a wait() and notify() example in the internet and found the following example.
In the following Code-Block, both getValue() and setValue Methods are synchronized. If i create two Threads and the first Thread gets the lock, isn't it true, that the second Thread will wait indefinitely, until the first Thread releases the Lock? So it is not possible, that getValue() and setValue() are run at the same time? If yes the wait() and notify() methods would be there for nothing.

tldr: How can getValue() and setValue() methods be called from different Threads at the same time?

public class Data {
    private int value;
    private boolean available = false;

    public synchronized int getValue() {
        while (!available) {
            try {
                wait();
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }
        available = false;
        notifyAll();
        return value;
    }

    public synchronized void setValue(int value) {
        while (available) {
            try {
                wait();
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }
        this.value = value;
        available = true;
        notifyAll();
    }
}

答案1

得分: 0

引用Object.wait的文档:

此方法会导致当前线程(称之为T)将自身置于此对象的等待集中,然后放弃对此对象的任何和所有同步声明。

本质上,wait是一种特殊情况,允许其他线程声明锁,直到它被通知。

英文:

Quoting the documentation for Object.wait:

> This method causes the current thread (call it T) to place itself in the wait set for this object and then to relinquish any and all synchronization claims on this object.

Essentially, wait is a special case that allows other threads to claim the lock until it is notified.

huangapple
  • 本文由 发表于 2023年2月19日 16:21:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75498835.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定