Interview prep: How to quickly create a java application that can deadlock!

Nowadays, almost every web or mobile application is a concurrent application.
What is a concurrent application?
In simple terms, an application which has more than a single thread of execution running simultaneously!
If you are a software developer out in the job market, you would feel confident if you have concepts related to concurrency on your fingertips!
My recent visit to job market validated this perception too :)
So, the above mentioned question was posed to me in a job interview. Well, I thought this would be too simple for me. I would just try to enforce circular wait between two java threads, which is one of the conditions which causes threads to deadlock.
So, without putting much thought I began to code it quickly, well guess what, when the code ran, there was NO deadlock!
So, in this small post, let’s try to ‘engineer’ a deadlock!

So, how do we create this scenario in java.
In this post, I plan to use synchronized
keyword in java to achieve this.
Let’s talk a bit about synchronized
keyword. In java, we can create explicit locks and implicit locks.
In simple language, for explicit locks, developer needs to write code to acquire and release locks while in case of implicit locks java takes care of it.
synchronized
keyword helps to acquire implicit locks over operands it operates on.
So, in java synchronized
keyword is used in synchronized methods and synchronized statements.
A java synchronized method may look like
What does synchronized
keyword in a method signature help us achieve:
- It ensures that if their are multiple threads trying to call this method on an object of
DemoClass
, only one of these threads will be able to call this particular method at a time i.e. synchronized keyword is helping mutual exclusion while executing this method.
How synchronized keyword provides mutual exclusion, as expected it does so by acquiring a lock! But, there is no object mentioned in lines 5 or 9 indicating object on which lock is acquired, right?
So, in case of synchronized
methods, a lock is acquired on object of class whose method it is..
What that means?
- Ina addition to no thread being able to call that method simultaneously on same object, it also ensures no other synchronized method can execute during the execution of a synchronized method, since they are blocked on acquiring object as a lock!!
This fact forms the main idea behind creating a deadlock scenario in this post.
Weaving a deadlock scenario-
Let’s suppose friends in our object-oriented world follow a simple protocol, if a friend Alice bows to her friend Bob, Bob always bows back to Alice.
Having said that, both Alice and Bob may bow to each other at any point in time, and the other friend will bow back as per friendship protocol :)
So, we are trying to create a deadlock as seen below:
Expectation: Since both aliceBowingThread
and bobBowingThread
threads almost start at same time, we expect to hit following scenario:
- t0:
aliceBowingThread
calls alice.bow() - t1:
bobBowingThread
calls bob.bow() - bob wants to bow back as per line 11, but can’t since
sychronized
methodbow()
called bybobBowingThread
, won’t release lock on Bob object until it completely executes. - similarly, alice wants to bow back as per line 11, but can’t since
sychronized
methodbow()
called byaliceBowingThread
, won’t release lock on Alice object until it completely executes.
So, summary is that:
- Thread
aliceBowingThread
holds a lock toAlice
object and waiting to acquire lock onBob
object to callbowBack()
to him. - Thread
bobBowingThread
holds a lock toBob
object and waiting to acquire lock onAlice
object to callbowBack()
to her.
I hope you are able to relate this scenario with Fig. 1 at this point.
Now, we can confirm its a deadlock if bow()
method is executed on both Alice and Bob objects but not bowBack()
.
But, when you run the above class Friend’s main() method, you will see output as
So, conceptually our code is correct then, why this may be happening?
The reason is that aliceBowingThread
is able to sequentially acquire locks on Alice
and Bob
objects before bobBowingThread
even acquires its first lock on Bob
object.
To avoid this race condition, let’s introduce a sleep time to bow()
method.
Output for updated Friend class in Ideone is
Thus, both threads have gotten blocked on resources(Alice and Bob objects) held by the other thread, creating a DEADLOCK state!
Do give your suggestions and ideas, happy coding!