Thread synchronization lock on class class and singleton mode

 

 

 

1 Understand the reason for the existence of two if (null == instance) judgments in the singleton pattern

2. Understand the way to download the lock on the .class (for those who cannot use the instance object to lock, for example, the following class is a singleton class, and can only be locked in .class)

 

/**
 * Singleton design pattern: ensure that a class has only one object
 * @author Administrator
 *
 */
public class SynDemo02 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		JvmThread thread1 = new JvmThread(100);
		JvmThread thread2 = new JvmThread(500);
		thread1.start();
		thread2.start();
		
	}

}
class JvmThread extends Thread{
	private long time;
	public JvmThread() {
	}
	public JvmThread(long time) {
		this.time =time;
	}
	@Override
	public void run() {		
		System.out.println(Thread.currentThread().getName()+"-->创建:"+Jvm.getInstance(time));
	}
}


/**
 * Singleton design pattern
 * Make sure a class has only one object
 * Lazy double checking
 * 1. The constructor is privatized to avoid external object creation directly
 * 2. Declare a private static variable
 * 3. Create an external public static method to access the variable. If the variable does not have an object, create the object
 */
class Jvm {
	//declare a private static variable
	private static Jvm instance =null;	
	// Constructor is privatized to avoid external object creation directly
	private Jvm(){
		
	}
	//Create an external public static method to access the variable, if the variable has no object, create the object
	public static Jvm getInstance(long time){
		//2 Then the thread cde comes in to visit, at this time the a thread has completed the instantiation of the instance, so cde now directly jumps out of the if judgment to get the instance object, which is the role of the existence of the if judgment
		if(null==instance){	//
			//1 The ab thread comes first, the instance is Null at this time, and then the ab thread comes in. If the a thread first acquires the lock, creates the instance object after entering, then releases the lock, and then the b thread comes in. At this time, if(null= =instance ) The success of the judgment allows the b thread to directly obtain the instance that has been created by a and return, which is the role of this if judgment
			synchronized(Jvm.class){
				if(null==instance ){
					try {
						Thread.sleep(time); // Delay, amplify the error, even if this delay is not set, there will be a delay in creating objects in practice.
					} catch (InterruptedException e) {
						e.printStackTrace ();
					}
					instance =new Jvm();
				}
			}
	  }//a
	  return instance;
	}
	
	
	public static Jvm getInstance3(long time){
		//abcde --> inefficient, any time multi-threading comes over, it needs to wait, and there are objects that need to wait
		synchronized(Jvm.class){
			if(null==instance ){
				try {
					Thread.sleep(time); //Delay, magnify the error
				} catch (InterruptedException e) {
					e.printStackTrace ();
				}
				instance =new Jvm();
			}
			return instance;
		}
	}
	
	
	public static synchronized Jvm getInstance2(long time){
		if(null==instance ){
			try {
				Thread.sleep(time); //Delay, magnify the error
			} catch (InterruptedException e) {
				e.printStackTrace ();
			}
			instance =new Jvm();
		}
		return instance;
	}
	
	
	
	public static Jvm getInstance1(long time){
		if(null==instance ){
			try {
				Thread.sleep(time); //Delay, magnify the error
			} catch (InterruptedException e) {
				e.printStackTrace ();
			}
			instance =new Jvm();
		}
		return instance;
	}
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326344944&siteId=291194637