Documentation

The Java™ Tutorials
Hide TOC
Starvation and Livelock饥饿与活锁
Trail: Essential Java Classes
Lesson: Concurrency
Section: Liveness

Starvation and Livelock饥饿与活锁

Starvation and livelock are much less common a problem than deadlock, but are still problems that every designer of concurrent software is likely to encounter.饥饿和活锁比死锁更不常见,但仍然是每个并发软件设计者可能遇到的问题。

Starvation饥饿

Starvation describes a situation where a thread is unable to gain regular access to shared resources and is unable to make progress. 饥饿描述了线程无法定期访问共享资源并且无法取得进展的情况。This happens when shared resources are made unavailable for long periods by "greedy" threads. 当“贪婪”线程使共享资源长时间不可用时,就会发生这种情况。For example, suppose an object provides a synchronized method that often takes a long time to return. 例如,假设一个对象提供了一个通常需要很长时间才能返回的同步方法。If one thread invokes this method frequently, other threads that also need frequent synchronized access to the same object will often be blocked.如果一个线程频繁调用此方法,那么也需要频繁同步访问同一对象的其他线程通常会被阻塞。

Livelock活锁

A thread often acts in response to the action of another thread. 一个线程经常响应另一个线程的操作。If the other thread's action is also a response to the action of another thread, then livelock may result. 如果另一个线程的操作也是对另一个线程的操作的响应,那么可能会产生活锁As with deadlock, livelocked threads are unable to make further progress. 与死锁一样,活锁线程无法取得进一步的进展。However, the threads are not blocked — they are simply too busy responding to each other to resume work. 但是,线程没有阻塞—他们只是忙着互相回复,无法继续工作。This is comparable to two people attempting to pass each other in a corridor: Alphonse moves to his left to let Gaston pass, while Gaston moves to his right to let Alphonse pass. 这相当于两个人试图在走廊里互相超越:阿尔方斯向左移动让加斯顿通过,而加斯顿向右移动让阿尔方斯通过。Seeing that they are still blocking each other, Alphone moves to his right, while Gaston moves to his left. 看到他们仍在互相阻挡,阿尔方斯移到了他的右边,而加斯顿移到了他的左边。They're still blocking each other, so...他们还在互相阻拦,所以……


Previous page: Deadlock
Next page: Guarded Blocks