c语言sscanf函数的用法是什么
253
2022-11-30
安全发布对象
发布对象:
import com.example.annoations.NotThreadSafe;import lombok.extern.slf4j.Slf4j;import java.util.Arrays;@Slf4j@NotThreadSafepublic 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@NotRecommendpublic 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;//懒汉模式,单例的实例在第一次使用的时候进行创建@NotThreadSafepublic 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;//恶汉模式,单例实例在类装载使用时创建@ThreadSafepublic class SingletonExample2 { //私有构造函数 private SingletonExample2(){ } //单例对象 private static SingletonExample2 instance=new SingletonExample2(); //静态的工厂方法 public static SingletonExample2 getInstance(){ return instance; }}
//懒汉模式,单例的实例在第一次使用的时候进行创建@NotThreadSafepublic 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;//懒汉模式,双重同步锁的单例模式,单例的实例在第一次使用的时候进行创建@NotThreadSafepublic 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;@ThreadSafepublic 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;//枚举模式 最安全@ThreadSafepublic 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@NotThreadSafepublic class ImmutableExample1{ private final static Integer a=1; private final static String b="2"; private final static Map
另外的不可变对象
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
import com.google.common.collect.Maps;import java.util.Collections;import java.util.Map;public class ImmutableExample2 { private static Map
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~