Is it possible for a thread to Deadlock itself?

后端 未结 20 1320
暗喜
暗喜 2020-12-02 09:29

Is it technically possible for a thread in Java to deadlock itself?

I was asked this at an interview a while back and responded that it wasn\'t possible but the inte

相关标签:
20条回答
  • 2020-12-02 09:54

    Maybe he meant LOCK itself, that's certainly too easy:

    synchronized( this )
    {
        wait( );
    }
    
    0 讨论(0)
  • 2020-12-02 09:54

    I know this is a old post. Here is another example how it could happen if your code has interaction with external resources:

    I have a thread, that open a database connection, start a transactionA, and begin update. The same thread, open another connection, start another transactionB. However, because transactionA hasn't committed yet, and it has database table locked, transactionB happens to access this locked table, so it has to wait

    In the end the same thread is block by itself because it opened up more than one database connection.


    This happened a lot in the application that I work with because there are many modules in the application, and a thread can run though many methods. These methods opens their own connections. Since we had different developers write their code, they may not see the how their code are begin called, and therefore couldn't see the overall database transactions that opened by the application.

    0 讨论(0)
提交回复
热议问题