Java 单例模式的实现资料整理
作者:超超boy 时间:2022-05-29 21:27:33
Java单例模式的实现,对java 单例模式的几种实现方法进行了整理:
单例模式好多书上都是这么写的:
public class SingleTon1 {
private SingleTon1(){
}
private static SingleTon1 instance = null;
public static SingleTon1 getInstance(){
if(instance == null){
instance = new SingleTon1();
}
return instance;
}
}
但是实际开发中是不会这么写的,因为有一个严重的问题,多线程并发访问的时候,可能会产生多个实例!!
下面列举几个常用的方法:
1.使用synchronized 关键字
package singleton;
public class SingleTon1 {
private SingleTon1(){
}
private static SingleTon1 instance = null;
//多线程问题解法一,但是效率不高!因为每次调用都会加锁!
public static synchronized SingleTon1 getInstance(){
if(instance == null){
instance = new SingleTon1();
}
return instance;
}
public void print(){
System.out.println("thread_id:"+Thread.currentThread().getId());
}
private static Object object = new Object();
//很巧妙的方法,只有在null的时候加锁,之后就不加啦
public static SingleTon1 getInstance2(){
if(instance == null){
synchronized (object){
instance = new SingleTon1();
}
}
return instance;
}
}
2.加锁
package singleton;
import java.util.concurrent.locks.ReentrantLock;
public class SingleTon2 {
private SingleTon2(){
}
private static ReentrantLock lock = new ReentrantLock();
private static SingleTon2 instance = null;
public void print(){
System.out.println("thread_id:"+Thread.currentThread().getId());
}
public static SingleTon2 getInstance2(){
if(instance == null){
lock.lock();
if(instance == null){ //注意这里还要判断下!!
instance = new SingleTon2();
}
lock.unlock();
}
return instance;
}
}
3.利用静态变量:
package singleton;
public class SingleTon3 {
public static void print(){
System.out.println("thread_id:"+Thread.currentThread().getId());
}
public static Nested getNested(){
return Nested.instance;
}
//这个是单例创建的类
static class Nested{
private Nested(){
}
static Nested instance = new Nested();
}
}
Test测试代码:
package singleton;
import singleton.SingleTon3.Nested;
public class Test2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Nested singleton;
Myrunnable mm = new Myrunnable();
Myrunnable m1 = new Myrunnable();
Myrunnable2 m2 = new Myrunnable2();
new Thread(m1).start();
new Thread(m2).start();
if(m1.singleton == m2.singleton){ //是同一个
System.out.println("是同一个");
}else{
System.out.println("不是同一个");
}
}
}
class Myrunnable implements Runnable{
Nested singleton;
@Override
public void run() {
// TODO Auto-generated method stub
singleton = SingleTon3.getNested();
SingleTon3.print();
}
}
class Myrunnable2 implements Runnable{
Nested singleton;
@Override
public void run() {
// TODO Auto-generated method stub
singleton = SingleTon3.getNested();
SingleTon3.print();
}
}
输出:
是同一个
thread_id:11
thread_id:10
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
来源:http://www.cnblogs.com/jycboy/p/5892185.html
标签:Java,单例模式
0
投稿
猜你喜欢
logback过滤部分日志输出的操作
2023-10-16 21:45:50
C# Chart控件标记问题详解
2023-01-23 03:37:35
spring mvc中直接注入的HttpServletRequst安全吗
2021-12-29 07:48:16
Java中继承thread类与实现Runnable接口的比较
2022-06-09 12:57:09
Android Tabhost使用方法详解
2023-10-02 12:46:46
C#将隐私信息(银行账户,身份证号码)中间部分特殊字符替换成*
2022-02-18 05:39:36
Android切面编程知识点详解
2023-05-05 04:18:04
C#实现扫描枪扫描二维码并打印(实例代码)
2023-02-26 18:02:56
Java数据结构之双向链表的实现
2023-09-17 16:21:45
C#中应用程序集的装载过程详解
2023-03-30 19:06:05
C语言实现顺序表的顺序查找和折半查找
2021-10-24 13:17:19
SSM项目使用拦截器实现登录验证功能
2023-06-17 16:12:38
Android实现ListView数据动态加载的方法
2021-11-02 23:01:02
Android开发中如何模拟输入
2022-03-02 02:07:18
Java 中String StringBuilder 与 StringBuffer详解及用法实例
2021-06-17 12:25:32
使用@RequestBody配合@Valid校验入参参数
2023-05-04 22:36:09
C# 将透明图片的非透明区域转换成Region的实例代码
2021-10-25 19:28:05
C#中使用DevExpress中的ChartControl实现极坐标图的案例详解
2022-12-05 06:30:39
java线程之用Thread类创建线程的方法
2023-02-09 18:35:07
Android自定义控件实现圆形进度条
2023-02-02 05:01:59