单例模式(singleton pattern)

什么是单例?

在整个应用程序中,某个类的实例只有一个.

class T{}
class Test{
    main:  获取到T的实例
    demo:  获取到T的实例
}

单例的应用

Spring中的bean默认都是单例
Servlet也是单例的
计算机的任务管理窗口

如何实现?

单例
    1.构造方法私有化
    2.在成员变量位置声明引用
    3.提供公有的static方法用于向外部提供实例

1.懒汉式单例 (lazySingleton) - 需要保证线程安全
public class Singleton {
    //构造方法私有化
    private Singleton(){}
    //只声明,不初始化
    private static Singleton singleton;
    //提供公有的static的同步synchronized的方法向外提供实例
    public synchronized static Singleton getInstance(){
        if (singleton==null){
            singleton = new Singleton();
        }
        return singleton;
    }
}
懒加载机制:什么时候用,什么是去拿/创建对象
    
2.饿汉式单例 - HungrySingleton 本身就是线程安全的
   public class Singleton{
       private Singleton(){}
       private static Singleton single=new Singleton();
       public static Singleton getInstance(){
           return single;
       } 
   } 

Synchronized回顾

用法:

Synchronized可以实现同步效果,是通过锁机制来保证线程安全的
    修饰方法,都是给调用方法的对象加锁

用法一:修饰方法
    - 修饰实例方法,会给调用当前方法的对象加锁,从而保证线程安全
    - 修饰static方法,会给类对象加锁,从而保证线程安全,
  
用法二:修饰代码块
    语法:
		synchronized(同步监视器对象){
            同步代码块
        }
		同步监视器对象由程序员自己制定,可以是任意对象,包括this
    
  建议修饰代码块,修饰代码块可以在保证线程安全的同时尽可能的提高并发执行效率.
       buy(){
            .....选衣服
            synchronized(试衣间){
                .....试衣服(按顺序来)
            }    
        }
    
实例变量  实例方法   -- 都是通过实例来调用的 
static变量  static方法  -- 都是通过类名来调用的

Synchronized应用场景

场景一:同步的
class T:   
	synchronized  demo(){}
class Test:  
	main:    
		T t = new T();    
		Thread th1 = new Thread(){        
            run:t.demo();  }        
		Thread th2 = new Thread(){        
            run:t.demo();  }    
		th1.start();    
		th2.start();
 
场景二:异步的
class T:   
	synchronized  demo(){}    
	test(){}
class Test: 
	main:        
		T t = new T();    
		T t1 = new T();    
		Thread th1 = new Thread(){                
            run: t.demo();}        
		Thread th2 = new Thread(){ 
        run:t1.demo();  
        }        
		th1.start();        
		th2.start();
场景三:异步的
class T:       
	synchronized  demo(){}    
	test(){}
class Test:  
	main:    
		T t = new T();    
		Thread th1 = new Thread(){        
            run:t.demo();  }        
		Thread th2 = new Thread(){                
            run:t.test();   }        
		th1.start();        
		th2.start();
场景四:同步的
class T:      
	synchronized  demo(){}   
	synchronized  test(){}
class Test:  
	main:    
		T t = new T();    
		Thread th1 = new Thread(){        
            run: t.demo();    }        
		Thread th2 = new Thread(){ 
        run:t.test(); 
        }        
		th1.start();        
		th2.start();
 
场景五:异步
class T:   
	synchronized  demo(){}   
	synchronized  test(){}
class Test:  
	main:    
		T t = new T();    
        T t1 = new T();    
		Thread th1 = new Thread(){            
            run:t.demo();    }        
		Thread th2 = new Thread(){           
            run:t1.test();    }        
		th1.start();       
		th2.start();

  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

半度纳

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值