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 interviewer told me that it is. Unfortunately I wasn't able to get his method on how to achieve this deadlock.
This got me thinking and the only situation that I can think of is where you can have this happen is where you have an RMI server process which contained a method that calls itself. The line of code that calls the method is placed in a synchronized block.
Is that even possible or was the interviewer incorrect?
The source code I was thinking about was along these lines (where testDeadlock is running in an RMI server process)
public boolean testDeadlock () throws RemoteException {
synchronized (this) {
//Call testDeadlock via RMI loopback
}
}
Maybe what the interviewer was thinking of was:
However I would argue that it does not count as a deadlock.
Ideally a thread should never create a deadlock itself using 'synchronized locks' unless there really is a bug in the JVM itself as 'allegedly' noticed by some people in older versions
Maybe he meant LOCK itself, that's certainly too easy:
The JVM only keeps track of the local thread that has the monitor, if the calling class makes an external call back in on itself the incoming call causes the original thread to deadlock itself.
You should be able to run this code to illustrate the idea
The output from the program looks like
Additionally the thread also dump shows the following
which indicates that the thread has indeed managed to lock itself
The interviewer was right. A thread can deadlock itself according to JCIP. But how?
In the section 2.3.2 of the JCIP we have the following paragraph about Reentrancy:
The synchronized keyword's lock is a reentrant lock so a thread can lock and unlock in a nested manner but if you use a non-reentrant lock like the following example I wrote as a proof. You will have a deadlock! According to JCIP.
The answer (Pram's) marked as correct isn't technically a deadlock as others have suggested. Its just blocked.
I would suggest in Java, you can lean on Java's definition (which is consistent with the two thread idea). The ultimate judge then can be the JVM if it detects the deadlock itself. So, in Pram's example, the thread would show something like the following if it was a geniune deadlock.
This deadlock detection has been available for intrinsic locks since 1.5 and
Lock
based cyclic deadlocks since 1.6.A continuously blocked resource, or at least something that is waiting for something that will never happen is called a livelock. Similar problems where processes outside the VM (for example) databases deadlocking are entirely possible but I'd argue not appropriate for the question.
I'd be interested in a write up of how the interviewer claims its possible...
In answer to your original question, it takes two to tango and I'd suggest Pram's answer shouldn't be marked as correct because its not! ;) The RMI thread which calls back can cause blocking but it runs on a different thread (managed by the RMI server) than that of main. Two threads are involved even if the main thread didn't explicitly set another one up. There is no deadlock as witnessed by the lack of the detection in the thread dump (or if you click 'detect deadlock' in jconsole), it'd be more accurately described as a livelock.
Having said all that, any discussion along the lines of each of these answers would be enough to satisfy me as an interviewer.