Deadlock detection in Java

后端 未结 16 1076
野的像风
野的像风 2020-11-28 20:21

Long time ago, I saved a sentence from a Java reference book: \"Java has no mechanism to handle deadlock. it won\'t even know deadlock occurred.\" (Head First Java 2nd E

16条回答
  •  生来不讨喜
    2020-11-28 21:08

    Java can detect deadlocks (although not at run-time, it can still diagnose and report it).

    For example, when using a slightly modified version of 'Saurabh M. Chande' code bellow (changed it to Java and added some timing to guarantee a lock on each run). Once you run it and it deadlocks, if you type:

    kill -3 PID   # where 'PID' is the Linux process ID
    

    It will generate a stack dump, which will include the following information:

    Found one Java-level deadlock:
    =============================
    "Thread-0":
         waiting to lock monitor 0x08081670 (object 0x7f61ddb8, a Deadlock$A),
         which is held by "main"
    "main":
          waiting to lock monitor 0x080809f0 (object 0x7f61f3b0, a Deadlock$B),
          which is held by "Thread-0"
    

提交回复
热议问题