将 `Thread.yield()` 插入同步函数时会发生什么

What will happen when insert the `Thread.yield()` into an synchronized function

我正在学习 Java 中的多线程。以下是演示代码,我很好奇Thread.yield()在函数内部的用法。

这不是一个 synchronized 函数,在 运行 任务完成它的工作之前不能调用它吗?那么在这个块中插入Thread.yield()和不插入有什么区别呢?

演示代码:

public class SynchronizeEvenGenerator {
private int currentEvenValue = 0;
/**
 * Generate even and return it
 * @return
 */
public synchronized int next() {
    ++currentEvenValue;
    Thread.yield();
    ++currentEvenValue;
    return currentEvenValue;
    }
}

Isn't it an synchronized function which cannot be called until the running task finish it's work on it ?

它不能运行在同一个对象的另一个线程中。

Then what is the diff between insert Thread.yield() into this block and not ?

正在 运行 线程的 CPU 可以上下文切换到系统上任何进程的另一个可用线程。

如果 运行 没有等待线程,它会使它慢大约 15 - 30 微秒。

c.f。 wait(0) 可以让另一个线程获得锁。

What will happen if Thread.yield() is called in a synchronized function?

Thread.yield()javadoc 所述:

"[This is a] hint to the scheduler that the current thread is willing to yield its current use of a processor. The scheduler is free to ignore this hint."

所以有两种可能:

  1. 没有任何反应;即 yield() 立即调用 return。
  2. 另一个线程被调度并开始执行。最终,这个线程被重新安排并且 yield() 调用 returns.

有一件事没有发生。线程 不会 放弃互斥体。碰巧被阻塞等待获取互斥锁的任何其他线程将保持被阻塞。


Isn't it a synchronized method, which cannot be called until the running task finishes its work on it?

Thread.yield 不是同步方法。 (即使是,它也会锁定 Thread 对象,而不是 synchronized 块当前持有的锁。)


因此,在您的示例中,对 next() 的调用保证使计数器恰好增加 2。如果其他线程调用 next() 方法,则第二次调用将保持阻塞状态,直到 (至少)在第一次通话后 returns.

javadoc 也这样说:

"It is rarely appropriate to use this method."


Another question: Will it become an deadlock for thread scheduling

没有。调用 yield() 的线程最终将被重新安排。

(死锁是一种非常特殊的现象(见Wikipedia article),只有在获取锁的时候才会发生,当线程让出时,既不获取锁也不释放锁,所以不会造成死锁。)

现在,当一个线程让步时,可能需要很长时间才能再次安排它,尤其是如果有许多其他可运行的线程具有相同的 或更高 优先级。最终结果是等待获取锁的其他线程可能会等待很长时间。这会过度增加争用和拥塞。但最终,yield() 调用将 return,next() 调用将 return,另一个线程将能够获取锁。

简而言之:在持有锁的情况下调用yield()对性能不利,但不会直接导致死锁。

正如 javadoc 所说,调用 yield() 很少是合适的。