Question About Deadlock Situation in Java

前端 未结 6 2287
鱼传尺愫
鱼传尺愫 2020-12-08 12:06

I\'m learning about deadlocks in Java, and there\'s this sample code from Sun\'s official tutorial:

Alphonse and Gaston are friends, and great belie

6条回答
  •  情书的邮戳
    2020-12-08 12:31

    I'd have to double check, but I think a synchronized method locks on the class object, so it locks out other synchronized methods in the same class.

    I think it locks on the class object itself, so it even blocks different instances.

    Edit to add:

    Take a look at this part of the java language spec

    Each bow method grabs it's own objects monitor. Both then try to call the other object's bow back, and block waiting for the other monitor.

提交回复
热议问题