码迷,mamicode.com
首页 > 其他好文 > 详细

单例模式

时间:2018-01-17 00:27:06      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:span   静态变量   单例模式   com   stat   package   static   out   ring   

 1 package com.jdk7.chapter2.singleton;
 2 
 3 public class SingletonA {
 4     //java虚拟机会加载静态变量
 5     private static int id = 1;
 6     private static SingletonA instance = new SingletonA();
 7     
 8     //私有构造函数,不被其他类调用创建类的对象
 9     private SingletonA(){}
10     
11     //同步获取id值,同一时刻只有一个线程进入该方法
12     public synchronized int getId() {
13         return id;
14     }
15     
16     public synchronized int next(){
17         return id++;
18     }
19     //在其他类中只能调用类名+getInstance方法获取类的对象
20     public static SingletonA getInstance() {
21         return instance;
22     };
23 }
 1 package com.jdk7.chapter2.singleton;
 2 
 3 public class SingletonB {
 4     //java虚拟机会加载静态变量
 5     private static int id = 1;
 6     private static SingletonB instance = null;
 7     
 8     //私有构造函数,不被其他类调用创建类的对象
 9     private SingletonB(){}
10     
11     //同步获取id值,同一时刻只有一个线程进入该方法
12     public synchronized int getId() {
13         return id;
14     }
15 
16     public synchronized int next(){
17         return id++;
18     }
19     
20     //只创建一次对象,提供给后面每次使用
21     public static SingletonB getInstance() {
22         if(instance==null){
23             instance = new SingletonB();
24         }
25         return instance;
26     }
27 }
 1 package com.jdk7.chapter2.singleton;
 2 
 3 public class SingleTest {
 4     public static void main(String[] args) {
 5         System.out.println("调用next方法前");
 6         SingletonA single1 = SingletonA.getInstance();
 7         SingletonA single2 = SingletonA.getInstance();
 8         System.out.println("single1: "+single1);
 9         System.out.println("single2: "+single2);
10         System.out.println("single1: "+single1.getId());
11         System.out.println("single2: "+single2.getId());
12         single1.next();
13         System.out.println("调用next方法后");
14         System.out.println("single1: "+single1.getId());
15         System.out.println("single2: "+single2.getId());
16         
17         SingletonB b1 = SingletonB.getInstance();
18         SingletonB b2 = SingletonB.getInstance();
19         System.out.println("调用next方法前");
20         System.out.println("b1: "+b1);
21         System.out.println("b2: "+b2);
22         System.out.println("b1: "+b1.getId());
23         System.out.println("b2: "+b2.getId());
24         System.out.println("调用next方法后");
25         b1.next();
26         System.out.println("b1: "+b1.getId());
27         System.out.println("b2: "+b2.getId());
28     }
29 }
30 
31 执行结果:
32 调用next方法前
33 single1: com.jdk7.chapter2.singleton.SingletonA@64b6be69
34 single2: com.jdk7.chapter2.singleton.SingletonA@64b6be69
35 single1: 1
36 single2: 1
37 调用next方法后
38 single1: 2
39 single2: 2
40 调用next方法前
41 b1: com.jdk7.chapter2.singleton.SingletonB@32728d
42 b2: com.jdk7.chapter2.singleton.SingletonB@32728d
43 b1: 1
44 b2: 1
45 调用next方法后
46 b1: 2
47 b2: 2

 

单例模式

标签:span   静态变量   单例模式   com   stat   package   static   out   ring   

原文地址:https://www.cnblogs.com/celine/p/8297276.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!