A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 廿乙木 中级黑马   /  2014-8-24 22:17  /  806 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. //饿汉式
  2. class duoxiancheng_15_3
  3. {
  4.         private int mun =4;
  5.         private static final duoxiangcheng_15_3 s =new duoxiancheng_15_3();
  6.         private duoxiancheng_15_3();
  7.         public static  duoxiancheng_15_3 mtget()
  8.         {
  9.                 return s;
  10.         }
  11.        
  12. }
  13. //懒汉式
  14. class duoxianchengxx_15_3
  15. {
  16.         private static duoxianchengxx_15_3 ss = null;
  17.         private  int num = 5;
  18.         private duoxianchengxx_15_3();
  19.         public static duoxianchengxx myget()
  20.         {
  21.         if(ss==null)
  22.                 ss = new duoxianchengxx_15_3();
  23.                 return ss;
  24.         }



  25. }
  26. //创建一个线程
  27. class duoxiancheng1 implements Runnable
  28. {
  29.         public void run()
  30.         {
  31.                 while(true)
  32.                 {
  33.                 System.out.println(duoxiancheng_15_3.mtget().mun);
  34.                 }
  35.         }
  36. }
  37. class duoxianchengDemo_15_3
  38. {
  39.        
  40.         duoxiancheng1 t = new duoxiancheng1();
  41.         Thread t1 = new Thread(t);
  42.         t1.start();
  43.         while(true)
  44.         {
  45.         System.out.println(duoxianchengxx_15_3.myget().num);
  46.         }


  47. }

复制代码


2 个回复

倒序浏览

//饿汉式

class duoxiancheng_15_3
{

        private int mun =4;

        private static final duoxiancheng_15_3 s =new duoxiancheng_15_3();

        private duoxiancheng_15_3(){};

        public static  duoxiancheng_15_3 mtget()

        {

                return s;

        }

        
}

//懒汉式

class duoxianchengxx_15_3

{

        private static duoxianchengxx_15_3 ss = null;

        private  int num = 5;

        private duoxianchengxx_15_3(){};

        public static duoxianchengxx_15_3 myget()

        {

        if(ss==null)

                ss = new duoxianchengxx_15_3();

                return ss;

        }




}

//创建一个线程

class duoxiancheng1 implements Runnable

{

        public void run()

        {

                while(true)

                {
                //既然你设了num是私有的,这里就不能用罗
               // System.out.println(duoxiancheng_15_3.mtget().num);

                }

        }

}

class duoxianchengDemo_15_3

{
        public void fun() {
                duoxiancheng1 t = new duoxiancheng1();

        Thread t1 = new Thread(t);

        t1.start();

        while(true)

        {

                System.out.println(duoxianchengxx_15_3.myget());

        }
        }



}
回复 使用道具 举报
把报的错帖上来。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马