首页 > 其他 > 详细

单例模式

时间:2014-11-05 22:45:43      阅读:276      评论:0      收藏:0      [点我收藏+]
 1 public class Singleton {
 2     
 3 private static Singleton singleton;
 4     //懒汉式单例
 5     private static Singleton singleton;
 6     private Singleton(){
 7     }
 8     
 9     public static synchronized Singleton getSingleton() {
10         if (singleton == null) {
11             singleton = new Singleton();
12         }
13         return singleton;
14     }
15 }    
1 public class Singleton {
2   private static Singleton singleton = new Singleton();
3     private Singleton(){
4     }
5     
6     public static synchronized Singleton getSingleton() {
7         return singleton;
8     }  
9 }

synchronized:线程同步(不允许多个线程同时访问该方法)

单例模式

原文:http://www.cnblogs.com/f644135318/p/4077357.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!