当前位置:网站首页>单例模式(singleton pattern)
单例模式(singleton pattern)
2022-08-02 14:22:00 【半度纳】
什么是单例?
在整个应用程序中,某个类的实例只有一个.
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();
边栏推荐
- DOM — 元素的增删改查
- 【数据读写】csv文件与xls/xlsx文件
- 数据源,分层开发以及jsp标签总结及相关代码
- CSV file with the data read and write 】 【 XLS/XLSX file
- 如何使用Swiper外部插件写一个轮播图
- VsCode更新后,怎么使用使用快捷键同时生成多个元素
- Impulse response invariant method and bilinear transformation method for IIR filter design
- 炎炎夏日打造一个属于自己的“便携小空调”吧
- lammps学习(二)联合原子模型聚乙烯拉伸
- Spark的概念、特点、应用场景
猜你喜欢
随机推荐
第六章-6.1-堆-6.2-维护堆的性质-6.3-建堆
为什么四个字节的float表示的范围比八个字节的long要广
Scala的模式匹配与样例类
小知识点系列:StringUtil.isEmpty()与StringUtil.isBlank()的区别
golang中使用泛型
Golang学习(三十五) go 连接redis
延时函数-定时器
FIR滤波器设计之窗函数法
【频域分析】频谱泄露、频率分辨率、栅栏效应
2022-07-18 第五小组 瞒春 学习笔记
Wigner-Ville distribution for time-frequency analysis
初识art-template模板引擎
2022-07-10 第五小组 瞒春 学习笔记
velocity模板页面四则运算
为什么float4个字节比long8个字节所表示的数值范围广
【数据知多少】一文学懂通过Tushare、AKshare、baostock、Ashare、Pytdx获取股票行情数据(含代码)
filebeat的配置
解决跨域的方法 --- Proxy
DOM - page rendering process
VsCode更新后,怎么使用使用快捷键同时生成多个元素