java多线程基本概述(十二)——多线程与单例

1:饿汉模式:

package soarhu;

class Singleton {

   private Singleton(){}

   private static final  Singleton SINGLETON = new Singleton();

   public static  Singleton getInstance(){
       return Singleton.SINGLETON;
   }
}
public class Test{
    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 10; i++) {
            new Thread(){
                @Override
                public void run() {
                    Singleton singleton = Singleton.getInstance();
                    System.out.println(singleton);
                }
            }.start();
        }
    }
}

输出结果:

soarhu.Singleton@f3f9f4b
soarhu.Singleton@f3f9f4b
soarhu.Singleton@f3f9f4b
soarhu.Singleton@f3f9f4b
soarhu.Singleton@f3f9f4b
soarhu.Singleton@f3f9f4b
soarhu.Singleton@f3f9f4b
soarhu.Singleton@f3f9f4b
soarhu.Singleton@f3f9f4b
soarhu.Singleton@f3f9f4b

Process finished with exit code 0

2:懒汉模式:

package soarhu;

class Singleton {

   private Singleton(){}

   private static volatile Singleton SINGLETON = null;

   public static  Singleton getInstance(){
       if (null==SINGLETON){
           synchronized (Singleton.class){
               if(null==SINGLETON){
                   SINGLETON=new Singleton();
               }
           }
       }
       return Singleton.SINGLETON;
   }
}
public class Test{
    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 10; i++) {
            new Thread(){
                @Override
                public void run() {
                    Singleton singleton = Singleton.getInstance();
                    System.out.println(singleton);
                }
            }.start();
        }
    }
}

输出结果:

soarhu.Singleton@1a4ffe2
soarhu.Singleton@1a4ffe2
soarhu.Singleton@1a4ffe2
soarhu.Singleton@1a4ffe2
soarhu.Singleton@1a4ffe2
soarhu.Singleton@1a4ffe2
soarhu.Singleton@1a4ffe2
soarhu.Singleton@1a4ffe2
soarhu.Singleton@1a4ffe2
soarhu.Singleton@1a4ffe2

Process finished with exit code 0

3:静置内部类:

package soarhu;

class Singleton {

   private Singleton(){}

   private  static class Holder{
       private static  Singleton  SINGLETON= new Singleton();
   }

   public static  Singleton getInstance(){
       return Holder.SINGLETON;
   }
}
public class Test{
    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 10; i++) {
            new Thread(){
                @Override
                public void run() {
                    Singleton singleton = Singleton.getInstance();
                    System.out.println(singleton);
                }
            }.start();
        }
    }
}

输出结果

soarhu.Singleton@1a4ffe2
soarhu.Singleton@1a4ffe2
soarhu.Singleton@1a4ffe2
soarhu.Singleton@1a4ffe2
soarhu.Singleton@1a4ffe2
soarhu.Singleton@1a4ffe2
soarhu.Singleton@1a4ffe2
soarhu.Singleton@1a4ffe2
soarhu.Singleton@1a4ffe2
soarhu.Singleton@1a4ffe2

Process finished with exit code 0

4:如果要保证对象序列化后仍然保持单利,那么代码如下:

package soarhu;

import java.io.*;

class Singleton implements Serializable{

    private static final long serialVersionUID = 123L;

    private Singleton(){}

    private  static class Holder{
        private static  Singleton  SINGLETON= new Singleton();
    }

    public static  Singleton getInstance(){
        return Holder.SINGLETON;
    }

    protected Object readResolve() throws ObjectStreamException{
        //diy serialize Strategy
        System.out.println("serialize");
        return getInstance();
    }


}
public class Test{
    public static void main(String[] args) throws Exception {
        Singleton singleton = Singleton.getInstance();
        FileOutputStream outputStream = new FileOutputStream(new File("d:\\a.bat"));
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
        objectOutputStream.writeObject(singleton);
        objectOutputStream.close();
        outputStream.close();
        System.out.println("writing object "+singleton);

        FileInputStream fileInputStream = new FileInputStream(new File("d:\\a.bat"));
        ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
        Object o = objectInputStream.readObject();
        objectInputStream.close();
        fileInputStream.close();
        if (Singleton.class.isInstance(o)){
            Singleton s2 =Singleton.class.cast(o);
            System.out.println("reading object "+s2);
        }
    }
}

输出结果:

writing object soarhu.Singleton@7f31245a
serialize
reading object soarhu.Singleton@7f31245a

Process finished with exit code 0

5:使用静态代码块实现单利

package soarhu;

class Singleton {

    private static  Singleton  SINGLETON = null;

    private Singleton(){}

   static {
        SINGLETON = new Singleton();
   }

    public static  Singleton getInstance(){
        return SINGLETON;
    }
}
public class Test{
    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 10; i++) {
            new Thread(){
                @Override
                public void run() {
                    Singleton singleton = Singleton.getInstance();
                    System.out.println(singleton);
                }
            }.start();
        }
    }
}

输出结果:

soarhu.Singleton@4763d146
soarhu.Singleton@4763d146
soarhu.Singleton@4763d146
soarhu.Singleton@4763d146
soarhu.Singleton@4763d146
soarhu.Singleton@4763d146
soarhu.Singleton@4763d146
soarhu.Singleton@4763d146
soarhu.Singleton@4763d146
soarhu.Singleton@4763d146

Process finished with exit code 0

 

posted @ 2017-04-19 14:22  soar_hu  阅读(164)  评论(0编辑  收藏  举报