有关Java死锁的问题求回答

今天复习多线程的时候,看到网上有这么一段代码,百思不得其解。望各位学长学姐指点

疑问1、为何调用bower.bowBack(this)无反应

疑问2、为何将System.out.format换成System.out.println就变成了循环重复执行bow、bowBack方法

package cn.edu.jyu.secure;

public class DeadLock {
    static class Friend {
        private final String name;

        Friend(String name) {
            this.name = name;
        }

        String getName() {
            return this.name;
        }

        synchronized void bow(Friend bower) {
            System.out.format("%s: %s"
                            + "  has bowed to me=====!%n",
                    this.name, bower.getName());
            bower.bowBack(this);
        }

        synchronized void bowBack(Friend bower) {
            System.out.println("进来了");
            System.out.format("%s: %s"
                            + " has bowed back to me----!%n",
                    this.name, bower.getName());
            bower.bow(this);
        }
    }

    public static void main(String[] args) {
        final Friend alphonse =
                new Friend("Alphonse");
        final Friend gaston =
                new Friend("Gaston");
        new Thread(() -> alphonse.bow(gaston)).start();
        new Thread(() -> gaston.bow(alphonse)).start();
    }
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值