Java Examples - Deadlock and Its Solution
A deadlock is a situation in which multiple threads are blocked at the same time, and one or all of them are waiting for a resource to be released. Because the threads are blocked indefinitely, the program cannot terminate normally.
The four necessary conditions for a Java deadlock:
- 1. Mutual exclusion: when a resource is used (occupied) by one thread, other threads cannot use it
- 2. No preemption: the resource requester cannot forcibly take the resource from the resource holder; the resource can only be released voluntarily by the holder.
- 3. Hold and wait: the resource requester holds the resources it already owns while requesting other resources.
- 4. Circular wait: there is a waiting queue — P1 holds P2's resource, P2 holds P3's resource, and P3 holds P1's resource. This forms a waiting cycle.
A deadlock is formed when all four conditions above hold. Of course, breaking any one of these conditions in a deadlock situation makes the deadlock disappear. The following Java code simulates how a deadlock arises.
Ways to solve the deadlock problem: one is to use synchronized, and the other is to use an explicit Lock.
However, if locks are used improperly and multiple objects need to be locked at the same time, a deadlock can occur, as follows:
LockTest.java file
import
java
.
util
.
Date
;
public
class
LockTest
{
public
static
String
obj1
=
"
obj1
"
;
public
static
String
obj2
=
"
obj2
"
;
public
static
void
main
(
String
[
]
args
)
{
LockA
la
=
new
LockA
(
)
;
new
Thread
(
la
)
.
start
(
)
;
LockB
lb
=
new
LockB
(
)
;
new
Thread
(
lb
)
.
start
(
)
;
}
}
class
LockA
implements
Runnable
{
public
void
run
(
)
{
try
{
System
.
out
.
println
(
new
Date
(
)
.
toString
(
)
"
LockA starts executing
"
)
;
while
(
true
)
{
synchronized
(
LockTest
.
obj1
)
{
System
.
out
.
println
(
new
Date
(
)
.
toString
(
)
"
LockA locks obj1
"
)
;
Thread
.
sleep
(
3000
)
;
//
Waiting here gives B a chance to lock
synchronized
(
LockTest
.
obj2
)
{
System
.
out
.
println
(
new
Date
(
)
.
toString
(
)
"
LockA locks obj2
"
)
;
Thread
.
sleep
(
60
1000
)
;
//
For testing purposes, it occupies and does not release
}
}
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
(
)
;
}
}
}
class
LockB
implements
Runnable
{
public
void
run
(
)
{
try
{
System
.
out
.
println
(
new
Date
(
)
.
toString
(
)
"
LockB starts executing
"
)
;
while
(
true
)
{
synchronized
(
LockTest
.
obj2
)
{
System
.
out
.
println
(
new
Date
(
)
.
toString
(
)
"
LockB locks obj2
"
)
;
Thread
.
sleep
(
3000
)
;
//
Waiting here gives A a chance to lock
synchronized
(
LockTest
.
obj1
)
{
System
.
out
.
println
(
new
Date
(
)
.
toString
(
)
"
LockB locks obj1
"
)
;
Thread
.
sleep
(
60
1000
)
;
//
For testing purposes, it occupies and does not release
}
}
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
(
)
;
}
}
}
The output of the above code is:
Tue May 05 10:51:06 CST 2015 LockB 开始执行
Tue May 05 10:51:06 CST 2015 LockA 开始执行
Tue May 05 10:51:06 CST 2015 LockB 锁住 obj2
Tue May 05 10:51:06 CST 2015 LockA 锁住 obj1
At this point a deadlock occurs.
To solve this problem, we do not use explicit locking; we use a semaphore for control.
A semaphore can control how many threads may access a resource. Here we specify that only one thread may access it, which achieves an effect similar to locking. A semaphore can also specify a timeout for acquisition, and we can do additional handling based on that timeout.
For cases where acquisition fails, the usual approach is to retry, specify a number of attempts, or exit immediately.
Let's look at the following code:
UnLockTest.java file
import
java
.
util
.
Date
;
import
java
.
util
.
concurrent
.
Semaphore
;
import
java
.
util
.
concurrent
.
TimeUnit
;
public
class
UnLockTest
{
public
static
String
obj1
=
"
obj1
"
;
public
static
final
Semaphore
a1
=
new
Semaphore
(
1
)
;
public
static
String
obj2
=
"
obj2
"
;
public
static
final
Semaphore
a2
=
new
Semaphore
(
1
)
;
public
static
void
main
(
String
[
]
args
)
{
LockAa
la
=
new
LockAa
(
)
;
new
Thread
(
la
)
.
start
(
)
;
LockBb
lb
=
new
LockBb
(
)
;
new
Thread
(
lb
)
.
start
(
)
;
}
}
class
LockAa
implements
Runnable
{
public
void
run
(
)
{
try
{
System
.
out
.
println
(
new
Date
(
)
.
toString
(
)
"
LockA starts executing
"
)
;
while
(
true
)
{
if
(
UnLockTest
.
a1
.
tryAcquire
(
1
,
TimeUnit
.
SECONDS
)
)
{
System
.
out
.
println
(
new
Date
(
)
.
toString
(
)
"
LockA locks obj1
"
)
;
if
(
UnLockTest
.
a2
.
tryAcquire
(
1
,
TimeUnit
.
SECONDS
)
)
{
System
.
out
.
println
(
new
Date
(
)
.
toString
(
)
"
LockA locks obj2
"
)
;
Thread
.
sleep
(
60
1000
)
;
//
do something
}
else
{
System
.
out
.
println
(
new
Date
(
)
.
toString
(
)
"
LockA failed to lock obj2
"
)
;
}
}
else
{
System
.
out
.
println
(
new
Date
(
)
.
toString
(
)
"
LockA failed to lock obj1
"
)
;
}
UnLockTest
.
a1
.
release
(
)
;
//
Release
UnLockTest
.
a2
.
release
(
)
;
Thread
.
sleep
(
1000
)
;
//
Try immediately; in reality "do something" is indeterminate
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
(
)
;
}
}
}
class
LockBb
implements
Runnable
{
public
void
run
(
)
{
try
{
System
.
out
.
println
(
new
Date
(
)
.
toString
(
)
"
LockB starts executing
"
)
;
while
(
true
)
{
if
(
UnLockTest
.
a2
.
tryAcquire
(
1
,
TimeUnit
.
SECONDS
)
)
{
System
.
out
.
println
(
new
Date
(
)
.
toString
(
)
"
LockB locks obj2
"
)
;
if
(
UnLockTest
.
a1
.
tryAcquire
(
1
,
TimeUnit
.
SECONDS
)
)
{
System
.
out
.
println
(
new
Date
(
)
.
toString
(
)
"
LockB locks obj1
"
)
;
Thread
.
sleep
(
60
1000
)
;
//
do something
}
else
{
System
.
out
.
println
(
new
Date
(
)
.
toString
(
)
"
LockB failed to lock obj1
"
)
;
}
}
else
{
System
.
out
.
println
(
new
Date
(
)
.
toString
(
)
"
LockB failed to lock obj2
"
)
;
}
UnLockTest
.
a1
.
release
(
)
;
//
Release
UnLockTest
.
a2
.
release
(
)
;
Thread
.
sleep
(
10
1000
)
;
//
This is only for demonstration, so tryAcquire waits just 1 second, and B must yield execution time to A; otherwise the two will deadlock forever
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
(
)
;
}
}
}
The output of the above example code is:
Tue May 05 10:59:13 CST 2015 LockA 开始执行
Tue May 05 10:59:13 CST 2015 LockB 开始执行
Tue May 05 10:59:13 CST 2015 LockB 锁住 obj2
Tue May 05 10:59:13 CST 2015 LockA 锁住 obj1
Tue May 05 10:59:14 CST 2015LockB 锁 obj1 失败
Tue May 05 10:59:14 CST 2015LockA 锁 obj2 失败
Tue May 05 10:59:15 CST 2015 LockA 锁住 obj1
Tue May 05 10:59:15 CST 2015 LockA 锁住 obj2
Java Examples