concurrency - Good example of livelock? -


I understand what is a living, but I was thinking that someone has a good code based example? And by code based, I mean no means "two people are trying to get away from each other in a corridor". If I read it, then I will lose my lunch. There is a very simple Java example of living where a husband and wife are trying to eat soup, but only one spoon is between them. Each husband is very polite, and the spoon will pass if the other has not eaten yet.

  public square livelock {spoon of static square} Private Diner boss; Public spoon (Diner D) {owner = D; } Public Diner getOwner () {Return owner; } Public synchronized zero set owner (Diner D) {owner = d; } Public synchronized usage () {System.out.printf ("% s has eaten!", Owner.name); }} Fixed square diner {private string name; Private boolean is Hungary; Public Diner (string n) {name = n; Hungarian = true; } Public string getName () {return name; } Public Boolean is Hungary () {return is Hungary; } With public zeros (spoonful spoon, dinner spouse or spouse) {While (hashangri) {// do not spoon, so wait patiently for the husband if (spoon. Boss! = This) {try {Thread. Sleep (1); } Grip (Interrupted Exception E) {continue; }                     to continue; } // If husband is hungry, insist on crossing spoon. If (husband / wife.shahinari ()) {System.out.printf ("% s: you are my dear first!% N", name, spouse .getName ()); Spoon.setOwner (spouse); to continue; } // husband was not hungry, so at the end eat spoon Use (); Hungary = False; System.out.printf ("% s: I'm stuffed, my dear% s!% N", name, husband .getName ()); Spoon.setOwner (spouse); }}} Public static zero main (string [] Args) {Last Diner husband = New Diner ("Bob"); Last Wife = New Diner ("Alice"); Last spoon = new spoon (husband); New thread (New Runnabal) (Public run from zero) (husband (wife, wife);}}) Start (); New thread (New Runnabal) (Public run from zero) (wife. E.D. (S, husband);}}). Start (); }}  

Comments