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

最简略的死锁

2012-09-24 
最简单的死锁public class Client {public static void main(String[] args) throws Throwable {List a

最简单的死锁

public class Client {public static void main(String[] args) throws Throwable {List a = new ArrayList();List b = new ArrayList();new Collector("collector1", a, b).collect();new Collector("collector2", b, a).collect();}static class Collector {private String name = "";private Object lockA = null;private Object lockB = null;public Collector(String name, Object a, Object b) {this.name = name;this.lockA = a;this.lockB = b;}public void collect() {new Thread(new Runnable() {@Overridepublic void run() {synchronized (lockA) {System.out.println(name + " got first...");try {Thread.currentThread().sleep(10);} catch (InterruptedException e) {}synchronized (lockB) {System.out.println(name + " got all.");}}}}, name).start();}}}

热点排行