当前位置:网站首页>Five ways of using synchronized to remove clouds and fog are introduced

Five ways of using synchronized to remove clouds and fog are introduced

2022-04-23 14:14:00 pureluckyfish

         The smaller the range of the lock , Minimal impact on code execution efficiency . The best way is to leave it unlocked , Concurrent programming is not always thread safe , Only when multiple threads share the same instance variable can thread safety problems occur . Non thread safety issues require locking for synchronization .

1、synchronized  Method

         Solved the problem of thread safety , But it affects the execution efficiency ;synchronized  Method   The range of the lock is the largest , So the execution efficiency is also the slowest .

synchronized public void printA() {
		try {
			System.out.println(" The thread name is :"+Thread.currentThread().getName()+" stay "+System.currentTimeMillis()+" Get into printA");
			Thread.sleep(3000);
			System.out.println(" The thread name is :"+Thread.currentThread().getName()+" stay "+System.currentTimeMillis()+" Leave printA");
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

2、synchronized static  Method

        every last *.java File corresponding to the class Class is singleton in memory .synchronized static  Method   It's right  *.java File corresponding Class Class object ;synchronized  Method   Is to lock the instance object of the class where the method is located , They are two different locks .

synchronized static void printC() {
		try {
			System.out.println(" The thread name is :"+Thread.currentThread().getName()+" stay "+System.currentTimeMillis()+" Get into printC");
			System.out.println(" The thread name is :"+Thread.currentThread().getName()+" stay "+System.currentTimeMillis()+" Leave printC");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

3、synchronized(xxx.class) Code block

        synchronized(xxx.class) Code block It can work on all object instances of the class .

public void printC() {
		synchronized (Service1.class) {
			System.out.println(" The thread name is :" + Thread.currentThread().getName() + " stay " + System.currentTimeMillis() + " Get into printC");
			System.out.println(" The thread name is :" + Thread.currentThread().getName() + " stay " + System.currentTimeMillis() + " Leave printC");
		}

	}

4、synchronized(this) Code block :

         Locked is the current object , contrast synchronized  Method , Reduced lock range , That is to reduce the scope of synchronization code , Thus, the efficiency of program execution is improved .

 public void println(String x) {
        synchronized (this) {
            print(x);
            newLine();
        }
    }

5、synchronized ( Not this object ) Code block

         The advantage is that there are two locks , Not with other synchronized(this) Scramble for this lock , Reduce the scope of synchronization , Greatly improve operation efficiency .

package com.yu.syn;

public class Service2 {
	
	private String usernameParam;
	private String passwordParam;

	private String anything = new String();

	public void setUsernamePassword(String username, String password) {
		String anything = new String();
		try {
			synchronized (anything) {
				System.out.println(" The thread name is :" + Thread.currentThread().getName() + " stay " + System.currentTimeMillis() + " Enter synchronization fast ");
				usernameParam = username;
				Thread.sleep(3000);
				passwordParam = password;
				System.out.println(" The thread name is :" + Thread.currentThread().getName() + " stay " + System.currentTimeMillis() + " Leave sync fast ");
			}

		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

}

版权声明
本文为[pureluckyfish]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231406486177.html