您的位置:首页 > 编程语言 > Java开发

The Java™ Tutorials — Concurrency :Starvation and Livelock 饥饿和活锁

2016-02-16 09:50 525 查看

The Java™ Tutorials — Concurrency :Starvation and Livelock 饥饿和活锁

原文地址:https://docs.oracle.com/javase/tutorial/essential/concurrency/starvelive.html

关键点

饥饿:一个急需此资源的线程长时间无法获得该资源

活锁:两个线程频繁地响应对方,进而导致双方的工作无法进行下去

如两个线程都想获得某互斥资源,但又同时为对方让路希望对方能够获取,让来让去没完没了,活锁发生了

全文翻译

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…

一个线程总是对另一个线程的行为进行反应。如果另一个线程的行为也是对其他线程进行反应,那么活锁也许就会发生。正如死锁那样,活锁也无法让工作进行下去。不过,这些线程并非被阻塞,它们只是为了能继续工作,在简单地忙于回复对方。我们可以将此同这个情况做个类比:两个人在走廊中相遇并希望通过,Alphonse往左移动以让Gaston通过,而Gaston却也往同一方向移动以让Alphonse通过。我们发现他们依旧被对方阻挡着。Alphone决定右移,而Gaston也做出了同样的动作,它们还是阻挡着对方,故….
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息