Is it possible for a thread to Deadlock itself?

后端 未结 20 1319
暗喜
暗喜 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:34

    You write a thread which can receive messages from other threads telling it to, for example, terminate. You write code in the thread to monitor other threads and send them terminate messages and wait for a response. The thread would find itself in the list, send itself a message to terminate and wait for itself to terminate. If it wasn't written in a way which prioritised incoming messages over the wait state ...

    0 讨论(0)
  • 2020-12-02 09:34

    If you stretch the definition of the term deadlock: a single thread can find itself blocked on a non-reentrant lock that it took earlier.

    0 讨论(0)
  • 2020-12-02 09:35

    You can get yourself into a single thread Deadlock with ReentrantReadWriteLock. Write locks can acquire read locks but not the other way around. The following will block indefinitely.

        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
        lock.readLock().lock();
        lock.writeLock().lock();
    
    0 讨论(0)
  • 2020-12-02 09:36

    Although the comments here are being pedantic about "deadlock" happening if at least two threads/actions are competing for the same resource...I think the spirit of this question was to discuss a need for Reentrant lock - especially in context of "recursive" locking

    Here's an example in python (I am certain the concept stays the same in Java): If you change the RLock to Lock (i.e. reentrant lock to lock, the thread will hang)

    import threading
    
    """
    Change RLock to Lock to make it "hang"
    """
    lock = threading.Condition(threading.RLock())
    
    
    def print_list(list):
        lock.acquire()
        if not list:
            lock.release()
            return
        print(list[0])
        print_list(list[1:])
        lock.release()
    
    
    print_list([1, 2, 3, 4, 5])
    
    0 讨论(0)
  • 2020-12-02 09:37

    A deadlock is a form of resource starvation with an interaction between multiple threads.

    When a thread gets into a state of resource staving itself, it is referred to a livelock which is similar to a deadlock, but not the same by definition.

    An example of a livelock is using ReentrantReadWriteLock. Despite being reentrant on reading OR writing, it doesn't allow upgrading the lock from read to write.

    public class LiveLock {
        public static void main(String[] args) {
            ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
            lock.readLock().lock();
            if (someCondition()) {
                // we want to write without allowing another thread to jump in.
                lock.writeLock().lock();
            }
        }
    
        private static boolean someCondition() {
            return true;
        }
    }
    

    results in the process blocking here

    "main" #1 prio=5 os_prio=0 tid=0x0000000002a52800 nid=0x550c waiting on condition [0x000000000291f000]
       java.lang.Thread.State: WAITING (parking)
        at sun.misc.Unsafe.park(Native Method)
        - parking to wait for  <0x00000007162e5e40> (a java.util.concurrent.locks.ReentrantReadWriteLock$NonfairSync)
        at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
        at java.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(AbstractQueuedSynchronizer.java:836)
        at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireQueued(AbstractQueuedSynchronizer.java:870)
        at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquire(AbstractQueuedSynchronizer.java:1199)
        at java.util.concurrent.locks.ReentrantReadWriteLock$WriteLock.lock(ReentrantReadWriteLock.java:943)
        at LiveLock.main(LiveLock.java:10)
    

    A related question is; can a thread get into a deadlock without creating additional threads. This is possible as there are background threads e.g. the finalizer thread, which can run user code in the background. This allows for the main thread and the finalizer thread to deadlock each other.

    0 讨论(0)
  • 2020-12-02 09:40

    Well, based on the definition of:

    A deadlock is a situation wherein two or more competing actions are each waiting for the other to finish.

    I would say that the answer is no - sure a thread can sit there waiting indefinitely for something, however unless two competing actions are waiting for each other it is by definition not a deadlock.

    Unless someone explains to me how a single thread can be simultaneously waiting for two actions to finish?

    UPDATE: The only possible situation that I can think of is some sort of message pump, where a thread processes a message that asks it to wait indefinitely for something to happen, where in fact that something will be processed by another message on the message pump.

    This (incredibly contrived) scenario could possibly be technically called a deadlock.

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