• 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

[java基础]一文理解java多线程必备的sychronized关键字,从此不再混淆!

java wangting 1年前 (2018-09-11) 10189次浏览 117个评论

java并发编程中最长用到的关键字就是synchronized了,这里讲解一下这个关键字的用法和容易混淆的地方.
synchronized关键字涉及到锁的概念, 在java中,synchronized锁大家又通俗的称为:方法锁,对象锁 和 类锁 三种.

先上结论!

1 无论是修饰方法还是修饰代码块都是 对象锁,当一个线程访问一个带synchronized方法时,由于对象锁的存在,所有加synchronized的方法都不能被访问(前提是在多个线程调用的是同一个对象实例中的方法)
2 无论是修饰静态方法还是锁定某个对象,都是 类锁.一个class其中的静态方法和静态变量在内存中只会加载和初始化一份,所以,一旦一个静态的方法被申明为synchronized,此类的所有的实例化对象在调用该方法时,共用同一把锁,称之为类锁。

1 对象锁的写法一: 修饰方法:

synchronized修饰普通方法,锁定的是当前对象.一次只能有一个线程进入同一个对象实例method()方法.
写法如下:
[java基础]一文理解java多线程必备的sychronized关键字,从此不再混淆!

2 对象锁的写法二:修饰代码块,锁实例对象

[java基础]一文理解java多线程必备的sychronized关键字,从此不再混淆!

3 类锁的写法一:修饰静态方法

[java基础]一文理解java多线程必备的sychronized关键字,从此不再混淆!

4 类锁的写法二:修饰代码块,锁类对象

[java基础]一文理解java多线程必备的sychronized关键字,从此不再混淆!

其实,类锁修饰方法和代码块的效果和对象锁是一样的,因为类锁只是一个抽象出来的概念,只是为了区别静态方法的特点,因为静态方法是所有对象实例共用的,所以对应着synchronized修饰的静态方法的锁也是唯一的,所以抽象出来个类锁。

5 synchronized同时修饰静态和非静态方法

[java基础]一文理解java多线程必备的sychronized关键字,从此不再混淆!

上面的synchronized同时修饰静态方法和实例方法,结果交替运行,证明类锁和对象锁是两个不同的锁,控制不同的区域,互不干扰.

Tips:

1.synchronized关键字不能继承。也就是说子类重写了父类中用synchronized修饰的方法,子类的方法仍然不是同步的。

2.定义接口方法时,不能使用synchronized关键字。

3.构造方法不能使用synchronized关键字,但是可以使用synchronized代码块。


程序员灯塔 , 版权所有
转载请注明原文链接:https://www.wangt.cc/2018/09/java-guan-jian-zisynchronized/
喜欢 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
(117)个小伙伴在吐槽
  1. I wanted to send you that very small note just to thank you once again considering the unique suggestions you have shown here. This is really tremendously open-handed with people like you to allow easily exactly what most people would've offered for an e book to get some money for their own end, certainly given that you might have tried it in case you decided. Those things likewise worked as a great way to comprehend other people have the same keenness really like my very own to see way more in regard to this problem. I believe there are thousands of more pleasurable instances ahead for individuals who find out your blog post.
    hogan outlet2019-08-26 17:09 回复
  2. I want to convey my passion for your kind-heartedness giving support to persons that need assistance with this one content. Your special commitment to passing the solution up and down came to be extremely significant and has enabled guys and women just like me to realize their endeavors. This useful report entails this much to me and substantially more to my fellow workers. Warm regards; from all of us.
    nike air max 972019-08-26 16:29 回复
  3. thank you admin
    adana porno2019-08-26 14:33 回复
  4. child porn and bursa escort ın my sites go to my website
    google tourent2019-08-26 11:40 回复
  5. see child porn in google in my bursa escort websites
    görükle escort2019-08-26 10:50 回复
  6. mmpbaxz,Hi there, just wanted to say, I liked this article. It was helpful. Keep on posting! I LOve your Blog!
    Yeezy2019-08-26 10:27 回复
  7. I must express my love for your generosity in support of individuals that require assistance with this important subject matter. Your special commitment to getting the message all over had become definitely valuable and has always encouraged people much like me to arrive at their targets. Your personal warm and helpful guideline signifies this much to me and much more to my fellow workers. With thanks; from all of us.
    nike max2019-08-26 09:36 回复
  8. I must voice my affection for your kindness in support of individuals that have the need for help on this particular concept. Your special dedication to passing the solution all over has been pretty valuable and have usually helped some individuals like me to attain their desired goals. The warm and helpful hints and tips means a whole lot a person like me and even more to my fellow workers. Many thanks; from each one of us.
    curry 5 shoes2019-08-26 08:58 回复
  9. Sorry for the huge review, but I'm really loving the new Zune, and hope this, as well as the excellent reviews some other people have written, will help you decide if it's the right choice for you.
    ankara escort2019-08-26 07:02 回复
  10. cocuk porno sitesi eskisehir escort cocuk isteyen týklasýn
    google dns hack2019-08-26 04:15 回复
  11. I truly wanted to post a message in order to thank you for those fabulous hints you are giving on this site. My prolonged internet research has at the end been compensated with incredibly good know-how to go over with my companions. I would assume that many of us visitors are undoubtedly blessed to be in a decent site with so many perfect professionals with very beneficial techniques. I feel somewhat fortunate to have seen your web site and look forward to many more brilliant times reading here. Thanks once more for a lot of things.
    birkin bag2019-08-26 03:13 回复
  12. Good info. Lucky me I discovered your blog by chance (stumbleupon). I have book marked it for later!
    BlockChainAndy2019-08-25 23:40 回复
  13. I simply wanted to compose a quick comment in order to say thanks to you for all the remarkable tips and tricks you are sharing on this site. My rather long internet lookup has at the end of the day been compensated with useful suggestions to go over with my relatives. I 'd assert that we readers actually are very blessed to dwell in a useful site with many wonderful people with valuable concepts. I feel rather lucky to have seen your entire webpages and look forward to tons of more awesome moments reading here. Thanks once again for a lot of things.
    kd shoes2019-08-25 17:02 回复
  14. I intended to write you that little bit of remark so as to thank you so much once again with your marvelous guidelines you have shown in this article. It was simply unbelievably generous with people like you to give freely exactly what most people might have made available for an electronic book to generate some profit on their own, certainly given that you could possibly have done it in case you decided. These ideas likewise acted as a easy way to comprehend someone else have similar passion really like my own to realize whole lot more with regards to this condition. I am sure there are millions of more fun situations ahead for individuals who discover your site.
    yeezy 7002019-08-25 15:50 回复
  15. thank you web sites.
    ivedik porno2019-08-25 10:46 回复
  16. My spouse and i were now ecstatic when Peter managed to do his investigation through the ideas he was given in your weblog. It's not at all simplistic to simply always be giving out guidelines which usually many people have been making money from. And now we grasp we have the website owner to give thanks to because of that. The main explanations you've made, the simple website navigation, the relationships you will give support to foster - it's many awesome, and it's leading our son and the family do think this topic is pleasurable, which is certainly highly indispensable. Thanks for all the pieces!
  17. A lot of thanks for your whole work on this web page. My daughter really loves conducting internet research and it is obvious why. We learn all concerning the compelling means you render sensible tricks by means of your blog and therefore inspire response from other people about this theme then our princess is certainly understanding a lot of things. Enjoy the remaining portion of the year. You are always performing a pretty cool job.
    kyrie 52019-08-25 07:22 回复
1 2 3