安全发布对象
发布对象:
import com.example.annoations.NotThreadSafe; import lombok.extern.slf4j.Slf4j; import java.util.Arrays; @Slf4j @NotThreadSafe public class UnsafePublish { private String[] states={"a","b","c"}; public String [] getStates(){ return states; } public static void main(String[] args) { UnsafePublish unsafePublish=new UnsafePublish(); log.info("{}", Arrays.toString(unsafePublish.getStates())); unsafePublish.getStates()[0]="d"; log.info("{}",Arrays.toString(unsafePublish.getStates())); } }
import com.example.annoations.NotRecommend; import com.example.annoations.NotThreadSafe; import lombok.extern.slf4j.Slf4j; @Slf4j @NotThreadSafe @NotRecommend public class Escape { public int thisCanBeEscape=0; public Escape(){ new InnerClass(); } private class InnerClass{ public InnerClass(){ log.info("{}",Escape.this.thisCanBeEscape); } } public static void main(String[] args) { new Escape(); } }
安全发布对象:
import com.example.annoations.NotThreadSafe; //懒汉模式,单例的实例在第一次使用的时候进行创建 @NotThreadSafe public class SingletonExample1 { //私有的构造函数 private SingletonExample1 (){ } //单例对象 private static SingletonExample1 instance=null; //静态的工厂方法 public static SingletonExample1 getInstance(){ if(instance==null){ instance=new SingletonExample1(); } return instance; } }
import com.example.annoations.ThreadSafe; //恶汉模式,单例实例在类装载使用时创建 @ThreadSafe public class SingletonExample2 { //私有构造函数 private SingletonExample2(){ } //单例对象 private static SingletonExample2 instance=new SingletonExample2(); //静态的工厂方法 public static SingletonExample2 getInstance(){ return instance; } }
//懒汉模式,单例的实例在第一次使用的时候进行创建 @NotThreadSafe public class SingletonExample3 { //私有的构造函数 private SingletonExample3 (){ } //单例对象 private static SingletonExample3 instance=null; //静态的工厂方法 public static synchronized SingletonExample3 getInstance(){ if(instance==null){ instance=new SingletonExample3(); } return instance; } }
线程不安全
import com.example.annoations.NotThreadSafe; //懒汉模式,双重同步锁的单例模式,单例的实例在第一次使用的时候进行创建 @NotThreadSafe public class SingletonExample4 { //私有的构造函数 private SingletonExample4 (){ } //1.memory=allocate(),分配内存空间 //2.ctorInstance()初始对象 //3.instance=memory 设置instance指向刚分配的内存 //JVM和cpu优化,发生了指令重拍 //1.memory=allocate(),分配内存空间 //3.instance=memory 设置instance指向刚分配的内存 //2.ctorInstance()初始对象 //单例对象 private static SingletonExample4 instance=null; //静态的工厂方法 public static SingletonExample4 getInstance(){ if(instance==null){//双重检测机制 synchronized (SingletonExample4.class){ //同步锁 if(instance==null){ //多线程发生指令重拍 instance=new SingletonExample4(); } } } return instance; } }
volatile+双重检测,线程安全
import com.example.annoations.ThreadSafe; @ThreadSafe public class SingletonExample5 { //私有的构造函数 private SingletonExample5(){ } //单例对象 volatile+双重检测机制 private volatile static SingletonExample5 instance=null; //静态的工厂方法 public static SingletonExample5 getInstance(){ if(instance==null){//双重检测机制 synchronized (SingletonExample5.class){ //同步锁 if(instance==null){ //多线程发生指令重拍 instance=new SingletonExample5(); } } } return instance; } }
//饿汉模式 public class SingletonExample6 { //私有构造函数 private SingletonExample6(){ } //单例对象 private static SingletonExample6 instance=null; static { instance=new SingletonExample6(); } //静态工厂的方法 public static SingletonExample6 getInstance(){ return instance; } public static void main(String[] args) { System.out.println(getInstance().hashCode()); System.out.println(getInstance().hashCode()); } }
import com.example.annoations.ThreadSafe; //枚举模式 最安全 @ThreadSafe public class SingletonExample7 { //私有构造函数 private SingletonExample7(){ } public static SingletonExample7 getInstance(){ return Singleton.INSTANCE.getInstance(); } public enum Singleton{ INSTANCE; //构造函数JVM保证这个方法只能被调用一次 private SingletonExample7 singleton; Singleton(){ singleton=new SingletonExample7(); } public SingletonExample7 getInstance(){ return singleton; } } }
import com.example.annoations.NotThreadSafe; import com.google.common.collect.Maps; import lombok.extern.slf4j.Slf4j; import java.util.Map; @Slf4j @NotThreadSafe public class ImmutableExample1{ private final static Integer a=1; private final static String b="2"; private final static Map<Integer,Integer> map = Maps.newHashMap(); static { map.put(1,2); map.put(3,4); map.put(5,6); } //值可修改 public static void main(String[] args) { map.put(1,3); log.info("{}",map.get(1)); } private void test(final int a){ // a=1; } }
另外的不可变对象
import com.example.annoations.ThreadSafe; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; @ThreadSafe //线程安全 public class ImmutableExample3 { private final static ImmutableList list=ImmutableList.of(1,2,3); private final static ImmutableSet set=ImmutableSet.copyOf(list); private final static ImmutableMap<Integer,Integer> map=ImmutableMap.of(1,2,3,4); private final static ImmutableMap<Integer,Integer> map2= ImmutableMap.<Integer,Integer>builder().put(3,4).put(5,6).build(); public static void main(String[] args) { list.add(4); map.put(1,4); System.out.println(map2.get(2)); } }
import com.google.common.collect.Maps; import java.util.Collections; import java.util.Map; public class ImmutableExample2 { private static Map<Integer,Integer> map = Maps.newHashMap(); static { map.put(1,2); map.put(3,4); map.put(5,6); map= Collections.unmodifiableMap(map); } //值可修改 public static void main(String[] args) { map.put(1,3); log.info("{}",map.get(1)); } }