皮一下很开心

上代码

package com.nice;



/**
 * @author nice
 */

 class Gril{

     private volatile static  Gril gril;
    
     /**
       *这里的参数名BOY应该是boy,因为之前写的是final类型忘了改回去
       */
     private static    Boy  BOY ;

     static {
         BOY = Boy.getBoy();
     }

     private Gril() {
         System.out.println("女朋友只能有一个,是私有的");
     }

    /**
     * 获得女朋友
     * @return Gril实体类
     */
    public static Gril getGril() {
         if (gril == null) {
             synchronized (Gril.class) {
                 gril = new Gril();
             }
         }
         else {
             throw new RuntimeException("您的男朋友已出轨");
         }
         return gril;
     }

    /**
     * 想男朋友了(递归引用,无限循环) 当然这里用wait notify会更好
     */
    public static   void thinkBoy() {
         System.out.println("我想我的男朋友了");
         try {
             Thread.sleep(520);
         } catch (InterruptedException e) {
             e.printStackTrace();
         }
        // Boy boy = Boy.getBoy();
         BOY.thinkGril();
     }


}

class Boy{

    private volatile static  Boy boy;

    private static    Gril  GRIL ;

    static {
        GRIL = Gril.getGril();
    }

    private Boy() {
        System.out.println("男朋友只能有一个,是私有的");
    }

    /**
     * 获得男朋友
     * @return Boy实体类
     */
    public static Boy getBoy() {
        if (boy == null) {
            synchronized (Gril.class) {
                boy = new Boy();
            }
        } else {
            throw new RuntimeException("您的女朋友已出轨");
        }
        return boy;
    }

    public static   void thinkGril() {
        System.out.println("我想我的女朋友了");
        try {
            Thread.sleep(520);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
//        Gril gril = Gril.getGril();
        GRIL.thinkBoy();
    }


}


public class LoveTest {

     public static void main(String[] args) {
         Boy boy = Boy.getBoy();
         boy.thinkGril();
     }

}

啊哈哈哈哈哈哈

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值