有人能举例说明一下死锁和活锁的区别吗?


当前回答

活锁

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, Alphonse moves to his right, while Gaston moves to his left. They're still blocking each other, and so on...

livelock和死锁之间的主要区别是线程不会被阻塞,相反,它们会尝试连续地相互响应。

在此图像中,两个圆圈(线程或进程)都将尝试通过左右移动为另一个圆圈提供空间。但是他们不能再前进了。

其他回答

摘自http://en.wikipedia.org/wiki/Deadlock:

In concurrent computing, a deadlock is a state in which each member of a group of actions, is waiting for some other member to release a lock A livelock is similar to a deadlock, except that the states of the processes involved in the livelock constantly change with regard to one another, none progressing. Livelock is a special case of resource starvation; the general definition only states that a specific process is not progressing. A real-world example of livelock occurs when two people meet in a narrow corridor, and each tries to be polite by moving aside to let the other pass, but they end up swaying from side to side without making any progress because they both repeatedly move the same way at the same time. Livelock is a risk with some algorithms that detect and recover from deadlock. If more than one process takes action, the deadlock detection algorithm can be repeatedly triggered. This can be avoided by ensuring that only one process (chosen randomly or by priority) takes action.

死锁 死锁是一种任务等待的情况 永远不可能发生的情况 满意 - task要求独占共享的控制权 资源 - task在等待其他任务时持有资源 待释放资源 —不能强制任务放弃资源 —存在循环等待条件

活锁 当两个或 更多的任务依赖和使用some 资源导致循环依赖 这些任务继续执行的条件 永远的奔跑,因此阻挡了一切的下降 优先级任务从运行(这些 低优先级任务出现问题 称为饥饿)

假设你有线程A和线程b,它们都在同一个对象上同步,在这个块中有一个全局变量,它们都在更新;

static boolean commonVar = false;
Object lock = new Object;

...

void threadAMethod(){
    ...
    while(commonVar == false){
         synchornized(lock){
              ...
              commonVar = true
         }
    }
}

void threadBMethod(){
    ...
    while(commonVar == true){
         synchornized(lock){
              ...
              commonVar = false
         }
    }
}

So, when thread A enters in the while loop and holds the lock, it does what it has to do and set the commonVar to true. Then thread B comes in, enters in the while loop and since commonVar is true now, it is be able to hold the lock. It does so, executes the synchronised block, and sets commonVar back to false. Now, thread A again gets it's new CPU window, it was about to quit the while loop but thread B has just set it back to false, so the cycle repeats over again. Threads do something (so they're not blocked in the traditional sense) but for pretty much nothing.

提到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, Alphonse moves to his right, while Gaston moves to his left. They're still blocking each other, and so on...

livelock和死锁之间的主要区别是线程不会被阻塞,相反,它们会尝试连续地相互响应。

在此图像中,两个圆圈(线程或进程)都将尝试通过左右移动为另一个圆圈提供空间。但是他们不能再前进了。

我只是想分享一些知识。

死锁 如果一组线程/进程中的每个线程/进程都在等待一个只有该组中的另一个进程才能引起的事件,则该组线程/进程被称为死锁。

这里重要的是另一个进程也在同一个集合中。这意味着另一个进程也被阻止了,没有人可以继续。

当进程被授予对资源的独占访问权时,就会发生死锁。

要产生死锁,必须满足这四个条件。

互斥条件(每个资源分配给1个进程) 保持和等待状态(处理持有资源,同时可以请求其他资源)。 没有抢占条件(以前授予的资源不能被强制拿走)#这个条件取决于应用程序 循环等待条件(必须是由2个或更多进程组成的循环链,每个进程都在等待链的下一个成员所持有的资源)#它将动态发生

如果我们发现了这些条件,那么我们就可以说可能发生了像死锁这样的情况。

活锁

每个线程/进程一次又一次地重复相同的状态,但没有进一步的进展。类似于死锁,因为进程不能进入临界区。然而,在死锁中,进程在等待而不做任何事情,但在livelock中,进程试图继续,但进程一次又一次地重复到相同的状态。

(在死锁计算中,没有可能成功的执行序列。在活锁计算中,有成功的计算,但有一个或多个执行序列中没有进程进入临界区。

不同于死锁和活锁

当死锁发生时,不会发生任何执行。但在livelock中,会发生一些执行,但这些执行不足以进入临界区。