- //饿汉式
- class duoxiancheng_15_3
- {
- private int mun =4;
- private static final duoxiangcheng_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 myget()
- {
- if(ss==null)
- ss = new duoxianchengxx_15_3();
- return ss;
- }
- }
- //创建一个线程
- class duoxiancheng1 implements Runnable
- {
- public void run()
- {
- while(true)
- {
- System.out.println(duoxiancheng_15_3.mtget().mun);
- }
- }
- }
- class duoxianchengDemo_15_3
- {
-
- duoxiancheng1 t = new duoxiancheng1();
- Thread t1 = new Thread(t);
- t1.start();
- while(true)
- {
- System.out.println(duoxianchengxx_15_3.myget().num);
- }
- }
复制代码
|
|