What is livelock?
2013-06-09 15:32
363 查看
http://docs.oracle.com/javase/tutorial/essential/concurrency/starvelive.html
后来看了看Wikipedia,上面也是讲得这个two people attempting to pass each other in a narrow corridor...
再去stackoverflow找,有一个提问的人也表示:能不能给个具体例子?链接如下
http://stackoverflow.com/questions/1036364/good-example-of-livelock
下面是个例子:
Here's a very simple Java example of livelock where a husband and wife are trying to eat soup, but only have one spoon between them. Each spouse is too polite, and will pass the spoon if the other has not yet eaten.
我把上面的例子改写了一下:
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...后来看了看Wikipedia,上面也是讲得这个two people attempting to pass each other in a narrow corridor...
再去stackoverflow找,有一个提问的人也表示:能不能给个具体例子?链接如下
http://stackoverflow.com/questions/1036364/good-example-of-livelock
下面是个例子:
Here's a very simple Java example of livelock where a husband and wife are trying to eat soup, but only have one spoon between them. Each spouse is too polite, and will pass the spoon if the other has not yet eaten.
public class Livelock { static class Spoon { private Diner owner; public Spoon(Diner d) { owner = d; } public Diner getOwner() { return owner; } public synchronized void setOwner(Diner d) { owner = d; } public synchronized void use() { System.out.printf("%s has eaten!", owner.name); } } static class Diner { private String name; private boolean isHungry; public Diner(String n) { name = n; isHungry = true; } public String getName() { return name; } public boolean isHungry() { return isHungry; } public void eatWith(Spoon spoon, Diner spouse) { while (isHungry) { // Don't have the spoon, so wait patiently for spouse. if (spoon.owner != this) { try { Thread.sleep(1); } catch(InterruptedException e) { continue; } continue; } // If spouse is hungry, insist upon passing the spoon. if (spouse.isHungry()) { System.out.printf("%s: You eat first my darling %s!%n", name, spouse.getName()); spoon.setOwner(spouse); continue; } // Spouse wasn't hungry, so finally eat spoon.use(); isHungry = false; System.out.printf("%s: I am stuffed, my darling %s!%n", name, spouse.getName()); spoon.setOwner(spouse); } } } public static void main(String[] args) { final Diner husband = new Diner("Bob"); final Diner wife = new Diner("Alice"); final Spoon s = new Spoon(husband); new Thread(new Runnable() { public void run() { husband.eatWith(s, wife); } }).start(); new Thread(new Runnable() { public void run() { wife.eatWith(s, husband); } }).start(); } }
我把上面的例子改写了一下:
package simple; class Spoon { private Diner owner; public Spoon(Diner diner) { owner = diner; } public Diner getOwner() { return owner; } public synchronized void setOwner(Diner diner) { owner = diner; } public synchronized void use() { System.out.printf("%s eating soup!", owner.getName()); } } class Diner { private String name; private Spoon spoon; private boolean isHungry; public boolean isHungry() { return isHungry; } public String getName() { return name; } public Diner(String name) { this.name = name; isHungry = true; } public void eatWith(Spoon spoon, Diner another) throws InterruptedException { synchronized (spoon) { // don't have the spoon, so wait patiently for another: while (spoon.getOwner() != this) { spoon.wait(); } // get the spoon, if 'another' is hungry, insist upon passing the spoon... while (another.isHungry()) { //! Here is what makes livelock happen: spoon.setOwner(another); System.out.println(name + ": you eat first, " + another.getName()); spoon.notifyAll(); spoon.wait(); } } // 'another' wasn't hungry, so finally eat: spoon.use(); isHungry = false; System.out.println(name + ": I'm stuffed"); spoon.setOwner(another); } } public class Foo { public static void main(String[] args) throws Exception { final Diner wife = new Diner("Amy"); final Diner husband = new Diner("Bob"); final Spoon spoon = new Spoon(wife); new Thread(new Runnable() { @Override public void run() { try { wife.eatWith(spoon, husband); } catch (InterruptedException e) { e.printStackTrace(); } } }).start(); new Thread(new Runnable() { @Override public void run() { try { husband.eatWith(spoon, wife); } catch (InterruptedException e) { e.printStackTrace(); } } }).start(); } }
相关文章推荐
- 活锁(livelock)
- 并发编程 活锁(livelock)
- UvaLive 6600 Spanning trees in a secure lock pattern 矩阵行列式
- Deadlock Livelock thread starvation
- Livelock
- UvaLive 6600 Spanning trees in a secure lock pattern 矩阵行列式
- uvalive 6600 - Spanning trees in a secure lock pattern
- 死锁(deadlock)、活锁(livelock)与饿死(starvation)
- UvaLive 6600 Spanning trees in a secure lock pattern 矩阵行列式
- deadlock vs livelock vs Starvation
- The Java™ Tutorials — Concurrency :Starvation and Livelock 饥饿和活锁
- what is great software
- what is WLAN ? when and why we need use it ?
- UVALive 4670 Dominating Patterns (AC自动机)
- Lock的await/singal 和 Object的wait/notify 的区别
- What is OHA?
- UVALive - 6151
- C# volatile与lock
- UVaLive 3905 Meteor (扫描线)
- 转载:CSDN、cnblogs使用Windows Live Writer客户端发布日志时相关配置