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也做出了同样的动作,它们还是阻挡着对方,故….
相关文章推荐
- C#线程间不能调用剪切板的解决方法
- C#线程同步的三类情景分析
- C#获取进程或线程相关信息的方法
- C#停止线程的方法
- C#子线程更新UI控件的方法实例总结
- C#线程队列用法实例分析
- C++使用CriticalSection实现线程同步实例
- 基于C++实现的线程休眠代码
- 探究在C++程序并发时保护共享数据的问题
- VB读取线程、句柄及写入内存的API代码实例
- C#网络编程基础之进程和线程详解
- C#通过Semaphore类控制线程队列的方法
- C#多线程处理多个队列数据的方法
- C#实现线程安全的简易日志记录方法
- C#中线程同步对象的方法分析
- Nodejs实战心得之eventproxy模块控制并发
- ASP.NET线程相关配置
- 浅析linux环境下一个进程最多能有多少个线程
- 再谈JavaScript线程
- C#实现终止正在执行的线程