Java单例模式实现的几种方式

作者:超超boy 时间:2021-06-09 18:38:47 

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
投稿

猜你喜欢

  • Android开发之自定义加载动画详解

    2023-07-27 01:41:05
  • android线程消息机制之Handler详解

    2023-07-28 11:31:52
  • Java Main 函数启动不退出的解决方案

    2022-03-24 14:25:03
  • JAVA SPI特性及简单应用代码实例

    2021-11-11 14:54:54
  • java高并发ScheduledThreadPoolExecutor与Timer区别

    2023-08-11 03:08:29
  • Spring BeanFactory和FactoryBean区别解析

    2023-09-18 15:38:28
  • 对Java ArrayList的自动扩容机制示例讲解

    2022-04-09 07:58:25
  • Java动态代 理分析及简单实例

    2023-11-24 21:14:56
  • 通过实例解析Socket套接字通信原理

    2023-11-02 20:17:35
  • springboot动态定时任务的实现方法示例

    2023-04-20 15:59:58
  • Java编程泛型限定代码分享

    2023-11-09 17:46:32
  • java实现微信扫码支付功能

    2023-11-09 18:38:08
  • Java多线程ThreadPoolExecutor详解

    2023-11-23 18:39:32
  • JAVA中的Token 基于Token的身份验证实例

    2023-11-09 18:05:09
  • Java基于Tcp的基础聊天功能实例

    2023-11-25 05:26:56
  • 配置Ant执行Jmeter脚本过程详解

    2023-11-09 22:35:29
  • java多线程CountDownLatch与线程池ThreadPoolExecutor/ExecutorService案例

    2021-06-21 12:29:50
  • MyBatis一对多嵌套查询的完整实例

    2023-07-12 02:49:56
  • Spring应用抛出NoUniqueBeanDefinitionException异常的解决方案

    2023-11-25 07:36:19
  • 详解java封装继承多态

    2023-11-24 08:29:37
  • asp之家 软件编程 m.aspxhome.com