Synchronising twice on the same object?

前端 未结 6 594
生来不讨喜
生来不讨喜 2020-12-13 01:45

I was wondering if in Java I would get any odd behaviour if I synchronise twice on the same object?

The scenario is as follows

pulbic class SillyClas         


        
6条回答
  •  死守一世寂寞
    2020-12-13 02:33

    No problems. In your example, (once you fix your code to get rid of the compile warnings that you'll get ;) ), the synchronization ensures that the blocks in method1 and method2 will not execute simultaneously.

    That's kind of the point of synchronization. :)


    Edit: Sorry, missed parts of your question, but Phill answered it. To summarize, a single thread cannot deadlock itself.

提交回复
热议问题