Good example of livelock?

后端 未结 11 1759
后悔当初
后悔当初 2021-01-29 18:44

I understand what livelock is, but I was wondering if anyone had a good code-based example of it? And by code-based, I do not mean \"two people trying to get p

11条回答
  •  感情败类
    2021-01-29 18:57

    I modify the answer of @jelbourn. When one of them notices that the other is hungry, he(her) should release the spoon and wait another notify, so a livelock happens.

    public class LiveLock {
        static class Spoon {
            Diner owner;
    
            public String getOwnerName() {
                return owner.getName();
            }
    
            public void setOwner(Diner diner) {
                this.owner = diner;
            }
    
            public Spoon(Diner diner) {
                this.owner = diner;
            }
    
            public void use() {
                System.out.println(owner.getName() + " use this spoon and finish eat.");
            }
        }
    
        static class Diner {
            public Diner(boolean isHungry, String name) {
                this.isHungry = isHungry;
                this.name = name;
            }
    
            private boolean isHungry;
            private String name;
    
    
            public String getName() {
                return name;
            }
    
            public void eatWith(Diner spouse, Spoon sharedSpoon) {
                try {
                    synchronized (sharedSpoon) {
                        while (isHungry) {
                            while (!sharedSpoon.getOwnerName().equals(name)) {
                                sharedSpoon.wait();
                                //System.out.println("sharedSpoon belongs to" + sharedSpoon.getOwnerName())
                            }
                            if (spouse.isHungry) {
                                System.out.println(spouse.getName() + "is hungry,I should give it to him(her).");
                                sharedSpoon.setOwner(spouse);
                                sharedSpoon.notifyAll();
                            } else {
                                sharedSpoon.use();
                                sharedSpoon.setOwner(spouse);
                                isHungry = false;
                            }
                            Thread.sleep(500);
                        }
                    }
                } catch (InterruptedException e) {
                    System.out.println(name + " is interrupted.");
                }
            }
        }
    
        public static void main(String[] args) {
            final Diner husband = new Diner(true, "husband");
            final Diner wife = new Diner(true, "wife");
            final Spoon sharedSpoon = new Spoon(wife);
    
            Thread h = new Thread() {
                @Override
                public void run() {
                    husband.eatWith(wife, sharedSpoon);
                }
            };
            h.start();
    
            Thread w = new Thread() {
                @Override
                public void run() {
                    wife.eatWith(husband, sharedSpoon);
                }
            };
            w.start();
    
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            h.interrupt();
            w.interrupt();
    
            try {
                h.join();
                w.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    

提交回复
热议问题