ITPub博客

首页 > 应用开发 > > Java- 请问如何使这段代码死锁?

Java- 请问如何使这段代码死锁?

作者:cdfoxman 时间:2019-08-19 09:25:04 0 删除 编辑

public class MicroBlongNode {

public static void main(String[] args) {
    final MicroBlongNode local = new MicroBlongNode("localhost:8888");
    final MicroBlongNode other = new MicroBlongNode("127.0.0.1:8988");
    final String first = "1";
    final String second = "2";

    new Thread(() -> local.propagate(first, other)).start();
    new Thread(() -> other.propagate(second,local)).start();
}

private final String ident;

public MicroBlongNode(String ident) {
    this.ident = ident;
}

public String getIdent() {
    return ident;
}

public synchronized void propagate(String upd, MicroBlongNode backup) {
    System.out.println(ident + ":recvd " + upd + "; backup:" + backup.getIdent());
    this.confirmUpdate(this, upd);
}

public synchronized void confirmUpdate(MicroBlongNode other, String upd) {
    System.out.println(ident + ":recvd confrim" + upd + "; from:" + other.getIdent());
}

}

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/625/viewspace-3118740/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-04-12

  • 博文量
    237
  • 访问量
    1739