Deadlock Starvation Livelock

Deadlock 死锁
Deadlock describes a situation where two or more threads are blocked forever, waiting for each other. Here's an example.
死锁就是俩个或多个线程互相等待造成永远被阻塞的状况。这里有一个例子
Alphonse and Gaston are friends, and great believers in courtesy. A strict rule of courtesy is that when you bow to a friend, you must remain bowed until your friend has a chance to return the bow. Unfortunately, this rule does not account for the possibility that two friends might bow to each other at the same time. This example application, Deadlock, models this possibility:
Alphonse和Gaston是朋友,并且非常讲礼貌。有礼的一个严苛的准则就是当你向你朋友鞠躬时,你必须保持鞠躬的姿势直到你朋友给你回应一个鞠躬。不幸的是,这个规则在俩个朋友在同一时间鞠躬时不适用。这个例子程序,Deadlock,模拟了这种可能性

public class Deadlock {
static class Friend {
private final String name;
public Friend(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public synchronized void bow(Friend bower) {
System.out.format("%s: %s"
+ " has bowed to me!%n",
this.name, bower.getName());
bower.bowBack(this);
}
public synchronized void bowBack(Friend bower) {
System.out.format("%s: %s"
+ " has bowed back to me!%n",
this.name, bower.getName());
}
}

public static void main(String[] args) {
final Friend alphonse =
new Friend("Alphonse");
final Friend gaston =
new Friend("Gaston");
new Thread(new Runnable() {
public void run() { alphonse.bow(gaston); }
}).start();
new Thread(new Runnable() {
public void run() { gaston.bow(alphonse); }
}).start();
}
}
When Deadlock runs, it's extremely likely that both threads will block when they attempt to invoke bowBack. Neither block will ever end, because each thread is waiting for the other to exit bow.
当Deadlock运行,当2个线程试图调用bowBack时非常有可能这2个线程会被阻塞。并且阻塞永远不会结束,因为每个线程都在等待其他的线程退出bow


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...

一个线程通常会有一些动作回应其他线程的活动。如果其他线程也会回应另一个线程的活动。这时就会导致活锁的发生。就像死锁,活锁也不能继续向前执行。然而线程并没有被阻塞-它们只是忙于彼此互相回应而不能继续工作。这可以比作有2个人在走廊上试图让彼此都通过:Alphonse移动他的左脚让Gaston通过,同时Gaston移动他的右脚让Alphonse通过。可以看到他们仍然互相阻塞,Alphone移动他右脚的时候,Gaston移动他的左脚。他们仍然互相阻塞在

原文地址:https://www.cnblogs.com/yuwenxing/p/2528751.html