当前位置:网站首页>Analysis of synchronized

Analysis of synchronized

2022-06-13 06:23:00 bearstwu

synchronized The most important function in threads is to protect thread safety , It is very easy to call data at the same time when the thread is processing , The main problem at this time is that the thread has been processed twice , But in both cases , In fact, I only got the answer once .

So in order to solve this problem , When dealing with a calculation problem that occurs in multiple threads at the same time, you must directly avoid other threads from getting the variable or formula . So in java More implemented in previous versions Volatile. There is a visibility problem when using . It is similar to the case that the hospital is now full of patients , If the ambulance is not visible, it may still bring patients to the hospital , But after arriving, I will find that the hospital is full of patients ,,Volatile This feature is well realized , But there are problems ,Volatile The scope of application is too small

1. Writing to a variable does not depend on the current value of the variable , Or you can make sure that only a single thread updates the value of the variable .
2. This variable is not included in invariants with other variables .
 

So let me list here volatile and synchronized The difference between

  1. volatile The essence is to tell jvm Current variable in register ( The working memory ) The value in is uncertain , Need to read from main memory ; synchronized Lock the current variable , Only the current thread can access this variable , Other threads blocked .
  2. volatile Can only be used at variable level ;synchronized You can use the 、 Method 、 And class level
  3. volatile Only variable modification visibility can be achieved , Atomicity is not guaranteed ; and synchronized It can ensure the visibility and atomicity of the variables
  4. volatile No thread blocking ;synchronized May cause thread blocking .
  5. volatile Tagged variables are not optimized by the compiler ;synchronized Tagged variables can be optimized by the compiler
public class AccountingSyncBad implements Runnable{
    static int i=0;
    public synchronized void increase(){
        i++;
    }
    @Override
    public void run() {
        for(int j=0;j<1000000;j++){
            increase();
        }
    }
    public static void main(String[] args) throws InterruptedException {
        //new New examples 
        Thread t1=new Thread(new AccountingSyncBad());
        //new New examples 
        Thread t2=new Thread(new AccountingSyncBad());
        t1.start();
        t2.start();
        //join meaning : Current thread A wait for thread After the thread is terminated, it can start from thread.join() return 
        t1.join();
        t2.join();
        System.out.println(i);
    }
}

In this case, thread safety can be guaranteed , But if you delete in the third line synchronized Words , The result is hard to be 20000, The greater probability of occurrence is less than 20000 but greater than or equal to 10000 The number of  

原网站

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