当前位置:网站首页>The difference between synchronize and volatile

The difference between synchronize and volatile

2022-06-25 22:39:00 No trace after wind 2018

synchronize and volatile The difference between

  1. volatile Can only be used to decorate variables ,synchronize Can be used to modify methods 、 Code block . With jdk Technological development ,synchronize The implementation efficiency will be greatly improved , therefore synchronize It is still common in the project process ;

  2. Multithreaded access volatile There will be no blockage ; and synchronize There will be a blockage ;

  3. volatile It can ensure the synchronization of variables between private memory and main memory , But the atomicity of variables cannot be guaranteed ;synchronize Variable atomicity can be guaranteed ;

  4. volatile Is the visibility of variables between threads ;synchronize Is the synchronization of accessing resources between multiple threads ; 
    about volatile Decorated variable , It can solve the visibility problem of variable reading , There is no guarantee of atomicity . For multiple threads accessing the same instance variable, lock synchronization is still required .

原网站

版权声明
本文为[No trace after wind 2018]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202180953065880.html