欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-03-12 18:35:56
java单例模式实现的几种方式 单例模式好多书上都是这么写的: public class singleton1 { private sing...

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

以上就是对java 单例模式的资料整理,后续继续补充相关资料,谢谢大家对本站的支持!