首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

简略死锁例子

2012-11-08 
简单死锁例子public class Deadlock {static class Friend {private final String namepublic Friend(Str

简单死锁例子

public class Deadlock {    static class Friend {        private final String name;        public Friend(String name) {            this.name = name;        }        public String getName() {            return this.name;        }        public synchronized void bow(Friend bower) {            System.out.format("%s: %s has bowed to me!%n",                     this.name, bower.getName());            bower.bowBack(this);        }        public synchronized void bowBack(Friend bower) {            System.out.format("%s: %s has bowed back to me!%n",                    this.name, bower.getName());        }    }    public static void main(String[] args) {        final Friend alphonse = new Friend("Alphonse");        final Friend gaston = new Friend("Gaston");        new Thread(new Runnable() {            public void run() { alphonse.bow(gaston); }        }).start();        new Thread(new Runnable() {            public void run() { gaston.bow(alphonse); }        }).start();    }}

热点排行