Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java中监视器的顺序_Java_Multithreading_Synchronization_Monitor - Fatal编程技术网

Java中监视器的顺序

Java中监视器的顺序,java,multithreading,synchronization,monitor,Java,Multithreading,Synchronization,Monitor,好吧,我错了-下面的陈述不适用,不适用于我的测试运行 这封来自Java线程邮件列表的邮件非常古老,实际上是1996年9月25日的。彼得·韦尔奇发现: 随附的一个演示表明,已通知的线程确实可以 放在队列的后面,以便重新获得监视器。之前 “wait”方法返回后,线程将在 在线程首次通过后很久才到达的线程后面进行监视 那个队!。这可能导致无限超车,因此, 线程饥饿 要再次总结该行为,请执行以下操作: Thread-1 acquires the monitor lock Thread-1 sees th

好吧,我错了-下面的陈述不适用,不适用于我的测试运行

这封来自Java线程邮件列表的邮件非常古老,实际上是1996年9月25日的。彼得·韦尔奇发现:

随附的一个演示表明,已通知的线程确实可以 放在队列的后面,以便重新获得监视器。之前 “wait”方法返回后,线程将在 在线程首次通过后很久才到达的线程后面进行监视 那个队!。这可能导致无限超车,因此, 线程饥饿

要再次总结该行为,请执行以下操作:

Thread-1 acquires the monitor lock
Thread-1 sees the condition is not true yet -> wait()
Thread-0 acquires the monitor lock
Thread-2 contends with Thread-0 for the monitor lock
Thread-3 contends with Thread-0 for the monitor lock
Thread-4 contends with Thread-0 for the monitor lock
Thread-5 contends with Thread-0 for the monitor lock
Thread-0 turns the condition to true -> notifyAll();
Thread-0 released the monitor lock
Thread-4 acquires the monitor lock
Thread-4 enjoys his desirable state
Thread-4 releases the monitor lock
Thread-2 acquires the monitor lock
Thread-2 enjoys his desirable state
...
第一个等待该条件的线程永远不会是第一个重新获得监视器的线程。我已经知道,没有公平的保证。然而,对我来说新的是,线程如何重新获得监视器存在某种顺序

为什么第一个线程应该是最后一个重新获得监视器锁的线程?线程1的实现方式永远无法通过条件并进入所需状态

这个语义有什么解释吗


重要提示:这个问题不是关于我是否可以依赖我发现的力学。我知道Java的等待和信号是如何记录的,它们清楚地表明,您不能依赖于此。我感兴趣的是,虚拟机是否以这种方式实现它,它们是否以这种特定的方式对线程进行排序。

如果使用对象的wait/notify,则排序将是JVM特有的。notify方法的javadoc声明:

Wakes up a single thread that is waiting on this object's monitor. If any threads are
waiting on this object, one of them is chosen to be awakened. The choice is arbitrary and 
occurs at the discretion of the implementation. A thread waits on an object's monitor by 
calling one of the wait methods.
但是,ReentrantReadWriteLock确实支持公平策略,公平模式描述为:

When constructed as fair, threads contend for entry using an approximately arrival
order policy. When the currently held lock is released either the longest-waiting
single writer thread will be assigned the write lock, or if there is a group of reader
threads waiting longer than all waiting writer threads, that group will be assigned the read lock.

我必须检查文档,但据我所知,无法保证几个竞争者中的哪个线程将获得锁。它不是先进先出或任何这样的确定性排序。如果希望函数X在函数Y之前执行,那么不要创建两个单独的线程。创建一个执行X的线程,然后执行Y。多线程的整体思想是所有这些线程独立运行。如果您问,我如何确保线程1在线程2之前运行?您问的问题是错误的。如果线程1中的代码必须在线程2中的代码之前运行,那么不要让它们成为两个单独的线程。这就像一个老笑话,病人:医生,每次我这样把胳膊举过头顶,我都会感到剧痛。医生:所以不要把你的手臂举过头顶。

对不起,我知道等待和信号是如何记录的,我已经在我的原始问题中添加了一个注释。如果我实现一个JVM,我会让数字3排在最后,我不喜欢那个数字。。。