谁确认在多线程中键是可用的?

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

Who acknowledges key is available in MultiThreading?

问题

我对同步块中的锁机制有些困惑。

每当一个 Thread 尝试锁定同步块时,它会寻找可用的键(可以是类级别的或实例级别的)来锁定同步块。

我想知道是哪个特定的实例告诉特定的线程该键是可用的?谁扮演着“接待员”的角色,以确认线程的请求以获得键,以便可以锁定同步块?

有人能分享一些简单的解释和示例吗?

英文:

I'm bit confused in lock mechanism in synchronized block.

When ever a Thread tries to lock a synchronized block, it looks for available key (could be a class level or instance level) to have a lock on synchronized block.

I want to know what's that specific instance which tells specific thread that the key is available ? Who plays the receptionist role to acknowledge the thread request to get a key so that it could have lock on synchronized block?

Can anyone share any simple explanation with some example ?

答案1

得分: 1

没有接待员,它的工作方式更像是一个卫生间:每个人进门后都会锁上门,如果门锁着就不会有人试图进来:

while 门是锁着的
    等待
锁上门
进行你的事情
解锁门

这只有在只有一个人能成功锁门的情况下才有效(即在我们放下裤子之前,我们会检查是否有人在卫生间里)。

从IT角度来看,JVM做了类似这样的操作:

class Monitor {
    AtomicReference owner = new AtomicReference();

    void lock() {
        do {
            boolean alone = owner.compareAndSet(null, Thread.currentThread());

            if (alone) {
                return;
            } 

            wait();
        } while (true);
    }

    void unlock() {
        if (owner.compareAndSet(Thread.currentThread(), null)) {
            return;
        } else {
            throw new IllegalMonitorStateException();
        }
    }
}

AtomicReference.compareAndSet 委托给一个特殊的硬件指令,只有在先前的值如预期般时才会更新内存位置。硬件确保此指令是原子的,即在执行此指令时,其他指令无法更改此内存位置。所使用的硬件指令的细节取决于JVM运行的硬件。

英文:

There is no receptionist, it works more like a bathroom: Everyone locks the door upon entering, and people don't try to enter the bathroom if the door is locked:

while door is locked
    wait
lock the door
do your business
unlock the door

That works provided that only a single person can succeed in locking the door (i.e. we check that we are alone in the bathroom before lowering our pants).

In IT terms, the JVM does something like this:

class Monitor {
    AtomicReference owner = new AtomicReference();

    void lock() {
        do {
            boolean alone = owner.compareAndSet(null, Thread.currentThread());

            if (alone) {
                return;
            } 

            wait();
        } while (true);
    }

    void unlock() {
        if (owner.compareAndSet(Thread.currentThread(), null)) {
            return;
        } else {
            throw new IllegalMonitorStateException();
        }
    }
}    

AtomicReference.compareAndSet delegates to a special hardware instruction that only updates a memory location if the previous value is as expected. The hardware ensures that this instruction is atomic, i.e. it is not possible for another instruction to change this memory location while this instruction executes. The particulars of the hardware instruction used depend on which hardware the JVM is running on.

huangapple
  • 本文由 发表于 2020年9月7日 19:13:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/63776475.html
匿名

发表评论

匿名网友

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

确定