Results 1 to 2 of 2
  1. #1
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    286
    Rep Power
    6

    Default Is this example of a Livelock situation correct?

    It does seem correct when you look at the output, but I am confused about the if blocks at Line 40 and Line 50. They could also be written as if-else construct, since path.personNotOnPath could wither be equal to this or not equal to this; I wrote them as separate if blocks because otherwise I was getting the "unreachable statement" error on Line 58. But logically, they (i.e. both the if blocks) are if-else in essence, right?

    And therefore the control would never reach the statement after both those if blocks anyway. And so I am thinking that probably my example of a livelock situation is faulty?

    Did I mess up somewhere? How could I modify it to make it correct?

    CODE:

    Java Code:
    /**
     * Simple example of Livelock
     * 
     * Let there be two persons walking on a one-lane corridor in opposite directions. When they encounter each other, they
     * are too polite to continue to walk, and so personOne moves to his left to give way to personTwo, who at the same time
     * moves right to give way to personOne; Seeing this, personOne moves to right, and meanwhile, personTwo also moves to
     * left; and so on...
     * 
     * @author 
     *
     */
    public class LivelockMyExampleTwo {
    
    	private static class Path {
    		private Person personNotOnPath;
    		
    		private synchronized void setPersonNotOnPath(Person person) {
    			personNotOnPath = person;
    		}
    	}
    
    	private static class Person {
    		private String name;
    		private boolean isEncountered;
    		private boolean onPathOrNot;
    		
    		Person (String name, boolean isEncountered, boolean onPathOrNot) {
    			this.name = name;
    			this.isEncountered = isEncountered;
    		}
    
    		private String getName() {
    			return name;
    		}
    
    		private synchronized void walkTheCorridor(Person otherPerson, Path path) {
    			while (isEncountered) {
    				// Not on the path, which means you have given way to the other person by moving aside, so wait
    				// patiently for the other person to get past by sleeping for 2 seconds.
    				if (path.personNotOnPath == this) {
    					try {
    						System.out.println(Thread.currentThread().getName() + "---> -----------------SLEEPING FOR 2 SECONDS-----------------");
    						Thread.sleep(2000);
    					} catch (InterruptedException ie) {
    						continue;
    					}
    					continue;
    				}
    				// On the path, so move aside and give the other person way.
    				if (path.personNotOnPath != this) {
    					System.out
    							.println(Thread.currentThread().getName() + " ---> " + name + ": Please go on, " + otherPerson.getName() + ", I am moving aside.");
    					path.setPersonNotOnPath(this); 
    					System.out.println(Thread.currentThread().getName() + " ---> " + name + "MOVES ASIDE.");
    					continue;
    				}
    
    				isEncountered = false;
    				System.out.println(Thread.currentThread().getName() + " ---> " + name
    						+ ": " + otherPerson.getName() + "was kind enough to give me way, so I am going ahead towards my destination");
    			}
    
    		}
    	}
    
    	public static void main(String[] args) {
    		final Person personOne = new Person("personOne", true, true);
    		final Person personTwo = new Person("personTwo", true, true);
    		
    		final Path path = new Path();
    
    		new Thread(new Runnable() {  public void run() {personOne.walkTheCorridor(personTwo, path);}  }).start();
    		new Thread(new Runnable() {  public void run() {personTwo.walkTheCorridor(personOne, path);}  }).start();
    	}
    }
    OUTPUT:

    Java Code:
    Thread-0 ---> personOne: Please go on, personTwo, I am moving aside.
    Thread-0 ---> personOneMOVES ASIDE.
    Thread-0---> -----------------SLEEPING FOR 2 SECONDS-----------------
    Thread-1 ---> personTwo: Please go on, personOne, I am moving aside.
    Thread-1 ---> personTwoMOVES ASIDE.
    Thread-1---> -----------------SLEEPING FOR 2 SECONDS-----------------
    Thread-0 ---> personOne: Please go on, personTwo, I am moving aside.
    Thread-1---> -----------------SLEEPING FOR 2 SECONDS-----------------
    Thread-0 ---> personOneMOVES ASIDE.
    Thread-0---> -----------------SLEEPING FOR 2 SECONDS-----------------
    Thread-1 ---> personTwo: Please go on, personOne, I am moving aside.
    Thread-0---> -----------------SLEEPING FOR 2 SECONDS-----------------
    Thread-1 ---> personTwoMOVES ASIDE.
    Thread-1---> -----------------SLEEPING FOR 2 SECONDS-----------------
    Thread-0 ---> personOne: Please go on, personTwo, I am moving aside.
    Thread-0 ---> personOneMOVES ASIDE.
    Thread-0---> -----------------SLEEPING FOR 2 SECONDS-----------------
    Thread-1 ---> personTwo: Please go on, personOne, I am moving aside.
    Thread-1 ---> personTwoMOVES ASIDE.
    Thread-1---> -----------------SLEEPING FOR 2 SECONDS-----------------
    
    ...

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,993
    Rep Power
    9

    Default Re: Is this example of a Livelock situation correct?

    The compiler is correct: if you have an if/else construction, line 58 is unreachable since there will ALWAYS be a continue statement executed. When you split them up, in two if's, then line 58 is STILL unreachable because of the opposite logic in the ifs, which results in again a continue every time, but the compiler doesn't make the relationship between the two ifs.

    Edit: drop the continue statements to make it work

    Edit 2: A variable boolean onPathOrNot is extremely confusing and very error prone. What does this mean: boolean onPathOrNot = true? Is the person on the path... Or maybe not? That's like asking: "do you want meat or fish", and then answering "yes".
    Last edited by SurfMan; 02-11-2016 at 02:25 PM.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

Similar Threads

  1. Term Vector files situation
    By luismpaiva in forum Lucene
    Replies: 0
    Last Post: 03-22-2012, 01:17 AM
  2. simple question: A multithread situation
    By simorgh in forum Threads and Synchronization
    Replies: 1
    Last Post: 01-13-2010, 09:25 PM
  3. What should the program look like for this situation
    By megironi in forum New To Java
    Replies: 4
    Last Post: 02-22-2009, 12:16 PM
  4. StringBuffer situation
    By orchid in forum New To Java
    Replies: 6
    Last Post: 08-12-2008, 02:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •