Java: Nested synchronization blocks

前端 未结 3 1834
心在旅途
心在旅途 2020-12-05 18:10

I saw this in one of Heinz Kabutz\'s Java Specialist newsletter editions and, although the rest (and indeed, all) of Dr. Kabutz\'s articles are well-explained and d

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-05 18:40

    There are 2 possible issues that one would have to watch out for

    1. Nested locks can result in deadlocks quite easily if one is using wait/notify. Here is an explanation of why. http://tutorials.jenkov.com/java-concurrency/nested-monitor-lockout.html

    2. One should be wary that if another method wishes to lock the same two objects, they must always do it in the same order, otherwise there is the possibility of another deadlock situation as explained in this post: How to avoid Nested synchronization and the resulting deadlock

提交回复
热议问题