Home  >  Article  >  Java  >  How to solve java singleton mode and thread safety issues

How to solve java singleton mode and thread safety issues

王林
王林forward
2023-05-12 23:07:041338browse

    Single case mode, multi-instance mode, and thread safety

    Single case mode

    Single case mode refers to ensuring that a class only There is a unique instance and provides a global access point.

    Category: Lazy Man Style, Hungry Man Style

    Why do we need singleton mode?

    In some special cases, it is necessary that a class can only be used to generate a unique object. For example: there are many printers in the printer room, but its print management system only has one print task control object, which manages the print queue and assigns print tasks to each printer. The singleton pattern was created to solve such needs.

    Implementation ideas:

    In order to prevent the client from using the constructor to create multiple objects, declare the constructor as a private type. But this will make this class unavailable, so a static method that can obtain an instance must be provided, usually called the getInstance method, which returns an instance. This method must be static, because static methods are called based on the class name, otherwise they cannot be used.

    Class diagram: Lazy Man style

    How to solve java singleton mode and thread safety issues

    ##Class diagram: Hungry Man style

    How to solve java singleton mode and thread safety issues

    Let’s take a look at a simple example first:

    Test singleton class: Dog’

    //懒汉式
    public class Dog {
    	private static Dog dog;
    	private String name;
    	private int age;
    	
    	//私有的构造器
    	private Dog() {}
    	
    	public String getName() {
    		return name;
    	}
    
    	public void setName(String name) {
    		this.name = name;
    	}
    
    	public int getAge() {
    		return age;
    	}
    
    	public void setAge(int age) {
    		this.age = age;
    	}
    
    	//静态工厂方法
    	public static Dog getInstance() {
    		if (dog == null) {
    			dog = new Dog();
    		}
    		return dog;
    	}
    
    	@Override
    	public String toString() {
    		return "Dog [name=" + name + ", age=" + age + "]";
    	}
    }

    Test singleton class :Cat

    //饿汉式
    public class Cat {
    	private static Cat cat = new Cat();
    	private String name;
    	private int age;
    	
    	//私有构造器
    	private Cat() {}
    	
    	public String getName() {
    		return name;
    	}
    
    	public void setName(String name) {
    		this.name = name;
    	}
    	
    	public int getAge() {
    		return age;
    	}
    	
    	public void setAge(int age) {
    		this.age = age;
    	}
    
    	//静态工厂方法
    	public static Cat getInstance() {
    		return cat;
    	}
    
    	@Override
    	public String toString() {
    		return "Cat [name=" + name + ", age=" + age + "]";
    	}
    }

    Test class

    import java.util.HashSet;
    import java.util.Set;
    
    public class Client {
    
    	public static void main(String[] args) {
    		//单线程模式测试
    		Dog dog1 = Dog.getInstance();
    		Dog dog2 = Dog.getInstance();
    		System.out.println("dog1 == dog2: "+(dog1 == dog2));
    		
    		Cat cat1 = Cat.getInstance();
    		Cat cat2 = Cat.getInstance();
    		System.out.println("cat1 == cat2: "+(cat1 == cat2));
    	}
    }

    Run result

    How to solve java singleton mode and thread safety issues

    Comparison between the lazy style and the hungry style

    Creation difference

    The lazy style is to create a singleton when the

    staticmethod getInstance() is called for the first time object. Hungry Chinese style is to create a singleton object when the class is loaded, that is, instantiate the singleton class when declaring a static singleton object.

    Thread safety

    The lazy style is thread-unsafe, while the hungry style is thread-safe (will be tested below).

    Resource occupation

    The lazy style is created when it is used, while the hungry style is created when the class is loaded. Therefore, the lazy man's style is not as fast as the hungry man's style, but the hungry man's style takes up more resources. If it is not used all the time, it will occupy a lot of resources.

    Safety in multi-threaded mode

    Multi-threaded class

    import java.util.HashSet;
    import java.util.Set;
    
    public class DogThread extends Thread{
    	private Dog dog;
    	private Set set;
    	
    	public DogThread() {
    		set = new HashSet<>();
    	}
    	
    	//这个方法是为了测试添加的。
    	public int getCount() {
    		return set.size();
    	}
    	
    	@Override
    	public void run() {
    		dog = Dog.getInstance();
    		set.add(dog);
    	}
    }

    Multi-threaded test class

    import java.util.HashSet;
    import java.util.Set;
    
    public class Client {
    
    	public static void main(String[] args) {
    		//单线程模式测试
    		Dog dog1 = Dog.getInstance();
    		Dog dog2 = Dog.getInstance();
    		System.out.println("dog1 == dog2: "+(dog1 == dog2));
    		
    		Cat cat1 = Cat.getInstance();
    		Cat cat2 = Cat.getInstance();
    		System.out.println("cat1 == cat2: "+(cat1 == cat2));
    		
    		//多线程模式测试
    		DogThread dogThread = new DogThread();
    		Thread thread = null;
    		for (int i = 0; i < 10; i++) {
    			thread = new Thread(dogThread);
    			thread.start();	
    		}
    		
    		try {
    			Thread.sleep(2000); //主线程等待子线程完成!
    		} catch (InterruptedException e) {
    			e.printStackTrace();
    		}
    		System.out.println("dog's number: "+dogThread.getCount());
    	}
    }

    Running results
    Note: The results of multi-threading are difficult to predict. Thread competition is involved here. The result may be the same multiple times (the same multiple times does not mean it is the same). Absolutely correct), but as long as you test it multiple times, you can see different results.

    How to solve java singleton mode and thread safety issues

    How to solve java singleton mode and thread safety issues

    ##Explanation

    Here I use a little set technique, using Set The characteristics of the collection are to store the dog object generated each time in the Set collection, and finally just call the size() method of the collection. It can be seen that two dog objects are generated, which means an error has occurred, which is a programming error. It is also important to understand that errors may not occur under multi-threading, so the generated dog objects are smaller than the number of threads.

    Since the Hungry-style singleton is thread-safe, it will not be tested here. If you are interested, you can test it.


    Solution to lazy singleton thread safety: synchronization

    Note: There are many methods of synchronization, and you can also use Lock for processing. Synchronization is a method, not a specific method. Those who are interested can explore more about the synchronzied keyword. And the synchronization method is usually slower, and performance must also be weighed.

    	//静态同步工厂方法
    	public synchronized static Dog getInstance() {
    		if (dog == null) {
    			dog = new Dog();
    		}
    		return dog;
    	}

    Multi-instance mode

    Here is a multi-instance mode, that is, the number of objects is a fixed number. You can see the promotion of the singleton pattern. Of course, there are many ways to implement it. You can try the following. Here is my way.

    Multi-instance mode class

    //固定数目实例模式
    public class MultiInstance {
    	//实例数量,这里为四个
    	private final static int INSTANCE_COUNT = 4;
    	private static int COUNT = 0;
    	private static MultiInstance[] instance = new MultiInstance[4];
    	
    	private MultiInstance() {};
    	
    	public static MultiInstance getInstance() {
    		//注意数组的下标只能为 COUNT - 1
    		if (MultiInstance.COUNT <= MultiInstance.INSTANCE_COUNT - 1) {
    			instance[MultiInstance.COUNT] = new MultiInstance();
    			MultiInstance.COUNT++;
    		}
    		//返回实例前,执行了 COUNT++ 操作,所以 应该返回上一个实例
    		return MultiInstance.instance[MultiInstance.COUNT-1];  
    	}
    }

    Test class

    import java.util.HashSet;
    import java.util.Set;
    
    public class Test {
    	public static void main(String[] args) {
    		
    		System.out.println("------------------------");
    		testMultiInstance();
    	}
    
    	//测试多实例模式(单例的扩展,固定数目实例)
    	public static void testMultiInstance() {
    		Set instanceSet = new HashSet<>();
    		MultiInstance instance = null;
    		for (int i = 0; i < 10; i++) {
    			instance = MultiInstance.getInstance();
    			instanceSet.add(instance);
    		}
    		System.out.println("8个实例中,不同的实例有:"+instanceSet.size());   
    	}
    }

    Running result

    Note : If used in a multi-threaded environment, thread safety must also be considered. If you are interested, you can implement it yourself.

    How to solve java singleton mode and thread safety issues

    #Is singleton mode necessarily safe?

    Not necessarily, there are many ways to break the singleton pattern!

    这里举例看一看(我只能举我知道的哈!其他的感兴趣,可以去探究一下!)
    使用反射:这种办法是非常有用的,通过反射即使是私有的属性和方法也可以访问了,因此反射破坏了类的封装性,所以使用反射还是要多多小心。但是反射也有许多其他的用途,这是一项非常有趣的技术(我也只是会一点点)。

    使用反射破坏单例模式测试类

    这里使用的还是前面的 Dog 实体类。注意我这里的**包名:**com。
    所有的类都是在 com包 下面的。

    import java.lang.reflect.Constructor;
    import java.lang.reflect.InvocationTargetException;
    
    public class Client {
    	public static void main(String[] args) throws 
    	ClassNotFoundException, 
    	NoSuchMethodException, 
    	SecurityException, 
    	InstantiationException, 
    	IllegalAccessException, 
    	IllegalArgumentException, 
    	InvocationTargetException {
    	
    		Class clazz = Class.forName("com.Dog");
    		Constructor con = clazz.getDeclaredConstructor();
    		//设置可访问权限
    		con.setAccessible(true);
    		Dog dog1 = (Dog) con.newInstance();
    		Dog dog2 = (Dog) con.newInstance();
    		System.out.println(dog1 == dog2);
    	}
    }

    说明:反射的功能是很强大的,从这里既可以看出来,正是有了反射,才使得Java 语言具有了更多的特色,这也是Java的强大之处。

    使用对象序列化破坏单例模式

    测试实体类:Dog(增加一个对象序列化接口实现)

    import java.io.Serializable;
    //懒汉式
    public class Dog implements Serializable{
    	private static final long serialVersionUID = 1L;
    	
    	private static Dog dog;
    	private String name;
    	private int age;
    	
    	//私有的构造器
    	private Dog() {}
    	
    	public String getName() {
    		return name;
    	}
    
    	public void setName(String name) {
    		this.name = name;
    	}
    
    	public int getAge() {
    		return age;
    	}
    
    	public void setAge(int age) {
    		this.age = age;
    	}
    
    	//静态工厂方法
    	public synchronized static Dog getInstance() {
    		if (dog == null) {
    			dog = new Dog();
    		}
    		return dog;
    	}
    
    	@Override
    	public String toString() {
    		return "Dog [name=" + name + ", age=" + age + "]";
    	}
    }

    对象序列化测试类

    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    
    public class Client {
    	public static void main(String[] args) throws IOException, ClassNotFoundException {
    		Dog dog1 = Dog.getInstance();
    		dog1.setName("小黑");
    		dog1.setAge(2);
    		System.out.println(dog1.toString());
    		
    		ByteArrayOutputStream bos = new ByteArrayOutputStream();
    		ObjectOutputStream oos = new ObjectOutputStream(bos);
    		oos.writeObject(dog1);
    		
    		
    		ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
    		ObjectInputStream ois = new ObjectInputStream(bis);
    		Dog dog2 = (Dog) ois.readObject();
    		System.out.println(dog2.toString());
    		System.out.println("dog1 == dog2: "+(dog1 == dog2));
    		
    	}
    }

    运行结果

    How to solve java singleton mode and thread safety issues

    说明
    这里可以看出来通过对象序列化(这里也可以说是对象的深拷贝或深克隆),
    同样也可以实现类的实例的不唯一性。这同样也算是破坏了类的封装性。对象序列化和反序列化的过程中,对象的唯一性变了。

    这里具体的原因很复杂,我最近看了点深拷贝的知识,所以只是知其然不知其之所以然。(所以学习是需要不断进行的!加油诸位。)
    这里我贴一下别的经验吧:(感兴趣的可以实现一下!)

    为什么序列化可以破坏单例了?
    答:序列化会通过反射调用无参数的构造方法创建一个新的对象。

    这个东西目前超出了我的能力范围了,但也是去查看源码得出来的,就是序列化(serializable)和反序列化(externalizable)接口的详细情况了。但是有一点,它也是通过反射来做的的,所以可以看出**反射(reflect)**是一种非常强大和危险的技术了。

    The above is the detailed content of How to solve java singleton mode and thread safety issues. For more information, please follow other related articles on the PHP Chinese website!

    Statement:
    This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete