How to detect deadlock ? Timeout in synchronized block?

后端 未结 7 2115
北荒
北荒 2020-12-29 09:00

I’m debugging a Java application that runs several threads. After a while of watching the log it seems that one of those threads is not running anymore. My guess is that the

7条回答
  •  渐次进展
    2020-12-29 09:21

    While it is possible have timeouts while waiting for locks on synchronized methods, implementing these is unwieldy. Basically you would spawn a timer thread which would interrupt the block thread after T seconds...not nice.

    If you're using Java 5 or higher I strongly suggest you see what the new concurrency classes offer. For example, you might consider using the ReentrantLock, which has a method tryLock(long timeout, TimeUnit unit) which will allow you to attempt to acquire the lock but allows you to escape after a fixed amount of time.

提交回复
热议问题