Simple Deadlock Examples - multithreading

I would like to explain threading deadlocks to newbies. I have seen many examples for deadlocks in the past, some using code and some using illustrations (like the famous 4 cars). There are also classic easily-deadlocked problems like The Dining Philosophers, but these may be too complex for a real newbie to fully grasp.
I'm looking for the simplest code example to illustrate what deadlocks are. The example should:
Relate to a "real" programming scenario that makes some sense
Be very short, simple and straight forward
What do you recommend?

Maybe a simple bank situation.
class Account {
double balance;
void withdraw(double amount){
balance -= amount;
}
void deposit(double amount){
balance += amount;
}
void transfer(Account from, Account to, double amount){
sync(from);
sync(to);
from.withdraw(amount);
to.deposit(amount);
release(to);
release(from);
}
}
Obviously, should there be two threads which attempt to run transfer(a, b) and transfer(b, a) at the same time, then a deadlock is going to occur because they try to acquire the resources in reverse order.
This code is also great for looking at solutions to the deadlock as well.

Here's a code example from the computer science department of a university in Taiwan showing a simple java example with resource locking. That's very "real-life" relevant to me. Code below:
/**
* Adapted from The Java Tutorial
* Second Edition by Campione, M. and
* Walrath, K.Addison-Wesley 1998
*/
/**
* This is a demonstration of how NOT to write multi-threaded programs.
* It is a program that purposely causes deadlock between two threads that
* are both trying to acquire locks for the same two resources.
* To avoid this sort of deadlock when locking multiple resources, all threads
* should always acquire their locks in the same order.
**/
public class Deadlock {
public static void main(String[] args){
//These are the two resource objects
//we'll try to get locks for
final Object resource1 = "resource1";
final Object resource2 = "resource2";
//Here's the first thread.
//It tries to lock resource1 then resource2
Thread t1 = new Thread() {
public void run() {
//Lock resource 1
synchronized(resource1){
System.out.println("Thread 1: locked resource 1");
//Pause for a bit, simulating some file I/O or
//something. Basically, we just want to give the
//other thread a chance to run. Threads and deadlock
//are asynchronous things, but we're trying to force
//deadlock to happen here...
try{
Thread.sleep(50);
} catch (InterruptedException e) {}
//Now wait 'till we can get a lock on resource 2
synchronized(resource2){
System.out.println("Thread 1: locked resource 2");
}
}
}
};
//Here's the second thread.
//It tries to lock resource2 then resource1
Thread t2 = new Thread(){
public void run(){
//This thread locks resource 2 right away
synchronized(resource2){
System.out.println("Thread 2: locked resource 2");
//Then it pauses, for the same reason as the first
//thread does
try{
Thread.sleep(50);
} catch (InterruptedException e){}
//Then it tries to lock resource1.
//But wait! Thread 1 locked resource1, and
//won't release it till it gets a lock on resource2.
//This thread holds the lock on resource2, and won't
//release it till it gets resource1.
//We're at an impasse. Neither thread can run,
//and the program freezes up.
synchronized(resource1){
System.out.println("Thread 2: locked resource 1");
}
}
}
};
//Start the two threads.
//If all goes as planned, deadlock will occur,
//and the program will never exit.
t1.start();
t2.start();
}
}

Let nature explain deadlock,
Deadlock: Frog vs. Snake
"I would love to have seen them go
their separate ways, but I was
exhausted," the photographer said.
"The frog was all the time trying to
pull the snake off, but the snake just
wouldn't let go".

If method1() and method2() both will be called by two or many threads, there is a good chance of deadlock because if thread 1 acquires lock on String object while executing method1() and thread 2 acquires lock on Integer object while executing method2() both will be waiting for each other to release lock on Integer and String to proceed further, which will never happen.
public void method1() {
synchronized (String.class) {
System.out.println("Acquired lock on String.class object");
synchronized (Integer.class) {
System.out.println("Acquired lock on Integer.class object");
}
}
}
public void method2() {
synchronized (Integer.class) {
System.out.println("Acquired lock on Integer.class object");
synchronized (String.class) {
System.out.println("Acquired lock on String.class object");
}
}
}

One of the simple deadlock example I have come across.
public class SimpleDeadLock {
public static Object l1 = new Object();
public static Object l2 = new Object();
public static void main(String[] a) {
Thread t1 = new Thread1();
Thread t2 = new Thread2();
t1.start();
t2.start();
}
private static class Thread1 extends Thread {
public void run() {
synchronized (l1) {
System.out.println("Thread 1: Holding lock 1...");
try { Thread.sleep(10); }
catch (InterruptedException e) {}
System.out.println("Thread 1: Waiting for lock 2...");
synchronized (l2) {
System.out.println("Thread 2: Holding lock 1 & 2...");
}
}
}
}
private static class Thread2 extends Thread {
public void run() {
synchronized (l2) {
System.out.println("Thread 2: Holding lock 2...");
try { Thread.sleep(10); }
catch (InterruptedException e) {}
System.out.println("Thread 2: Waiting for lock 1...");
synchronized (l1) {
System.out.println("Thread 2: Holding lock 2 & 1...");
}
}
}
}
}

Here's a simple example in C++11.
#include <mutex> // mutex
#include <iostream> // cout
#include <cstdio> // getchar
#include <thread> // this_thread, yield
#include <future> // async
#include <chrono> // seconds
using namespace std;
mutex _m1;
mutex _m2;
// Deadlock will occur because func12 and func21 acquires the two locks in reverse order
void func12()
{
unique_lock<mutex> l1(_m1);
this_thread::yield(); // hint to reschedule
this_thread::sleep_for( chrono::seconds(1) );
unique_lock<mutex> l2(_m2 );
}
void func21()
{
unique_lock<mutex> l2(_m2);
this_thread::yield(); // hint to reschedule
this_thread::sleep_for( chrono::seconds(1) );
unique_lock<mutex> l1(_m1);
}
int main( int argc, char* argv[] )
{
async(func12);
func21();
cout << "All done!"; // this won't be executed because of deadlock
getchar();
}

Please see my answer to this question. Bottom line whenever two threads need to acquire two different resources, and do so in different orders then you can get deadlocks.

I recently realized that the fights between couples are nothing but a deadlock.. where usually one of the process has to crash to resolve it, of course it's the lesser priority one(Boy ;)).
Here's the analogy...
Process1: Girl(G) Process2: Boy(B)
Resource1: Sorry Resource2: Accepting own mistake
Necessary Conditions:
1. Mutual Exclusion: Only one of G or B can say sorry or accept own Mistake at a time.
2. Hold and Wait: At a time, one is holding Sorry and other Accepting own mistake, one is waiting for Accepting own mistake to release sorry, and other is waiting for sorry to release accepting own mistake.
3. No preemption: Not even God can force B or G to release Sorry or Accepting own mistake. And voluntarily? Are you kidding me??
4. Circular Wait: Again, the one holding sorry waits for other to accept own mistakes, and one holding accept own mistakes want other to say sorry first. So it's circular.
So deadlocks occur when all these conditions are in effect at the same time, and that's always the case in a couple fight ;)
Source: http://www.quora.com/Saurabh-Pandey-3/Posts/Never-ending-couple-fights-a-deadlock

One example I can think of is the Table, Flashlight, and Batteries scenario. Imagine a flashlight and a pair of batteries placed on top of a table. If you were to walk to this table and grab the batteries while another person has the flashlight you both will be forced to awkwardly stare at each other while waiting for who will first place their item back on the table. This is an example of deadlock. You and the person are waiting for resources but none of you are giving up their resource.
Similarly, in a program, deadlock occurs when two or more threads (you and the other person) are waiting for two or more locks (flashlight and batteries) to be freed and the circumstances in the program are such that the locks are never freed (you both have one piece of the puzzle).
If you know java, this is how you can represent this problem:
import java.util.concurrent.locks.*;
public class Deadlock1 {
public static class Table {
private static Lock Flashlight = new ReentrantLock();
private static Lock Batteries = new ReentrantLock();
public static void giveFlashLightAndBatteries() {
try {
Flashlight.lock();
Batteries.lock();
System.out.println("Lights on");
} finally {
Batteries.unlock();
Flashlight.unlock();
}
}
public static void giveBatteriesAndFlashLight() {
try {
Batteries.lock();
Flashlight.lock();
System.out.println("Lights on");
} finally {
Flashlight.unlock();
Batteries.unlock();
}
}
}
public static void main(String[] args) {
// This thread represents person one
new Thread(new Runnable() {
public void run() { Table.giveFlashLightAndBatteries(); }
}).start();
// This thread represents person two
new Thread(new Runnable() {
public void run() { Table.giveBatteriesAndFlashLight(); }
}).start();
}
}
If you run this example you will notice that sometimes things work nice and correctly. But sometimes your program will just not print anything. That is because one person has the batteries while another person has the flashlight which prevents them from turning on the flashlight causing a deadlock.
This example is similar to the example given by the java tutorials: http://docs.oracle.com/javase/tutorial/essential/concurrency/deadlock.html
Another example is the loop example:
public class Deadlock2 {
public static class Loop {
private static boolean done = false;
public static synchronized void startLoop() throws InterruptedException {
while(!done) {
Thread.sleep(1000);
System.out.println("Not done");
}
}
public static synchronized void stopLoop() {
done = true;
}
}
public static void main(String[] args) {
// This thread starts the loop
new Thread(new Runnable() {
public void run() {
try {
Loop.startLoop();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
// This thread stops the loop
new Thread(new Runnable() {
public void run() {
Loop.stopLoop();
}
}).start();
}
}
This example can either print 'Not done' over and over or it can never print 'Not done' at all. The first happens because the first thread acquires the class lock and never releases it preventing 'stopLoop' from being accessed by the second thread. And the latest happens because the second thread started before the first thread causing the 'done' variable to be true before the first thread executes.

public class DeadLock {
public static void main(String[] args) throws InterruptedException {
Thread mainThread = Thread.currentThread();
Thread thread1 = new Thread(new Runnable() {
#Override
public void run() {
try {
mainThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
thread1.start();
thread1.join();
}
}

I consider the Dining Philosophers problem to be one of the more simple examples in showing deadlocks though, since the 4 deadlock requirements can be easily illustrated by the drawing (especially the circular wait).
I consider real world examples to be much more confusing to the newbie, though I can't think of a good real world scenario off the top of my head right now (I'm relatively inexperienced with real-world concurrency).

One more simple deadlock example with two different resources and two thread waiting for each other to release resource. Directly from examples.oreilly.com/jenut/Deadlock.java
public class Deadlock {
public static void main(String[] args) {
// These are the two resource objects we'll try to get locks for
final Object resource1 = "resource1";
final Object resource2 = "resource2";
// Here's the first thread. It tries to lock resource1 then resource2
Thread t1 = new Thread() {
public void run() {
// Lock resource 1
synchronized(resource1) {
System.out.println("Thread 1: locked resource 1");
// Pause for a bit, simulating some file I/O or something.
// Basically, we just want to give the other thread a chance to
// run. Threads and deadlock are asynchronous things, but we're
// trying to force deadlock to happen here...
try { Thread.sleep(50); } catch (InterruptedException e) {}
// Now wait 'till we can get a lock on resource 2
synchronized(resource2) {
System.out.println("Thread 1: locked resource 2");
}
}
}
};
// Here's the second thread. It tries to lock resource2 then resource1
Thread t2 = new Thread() {
public void run() {
// This thread locks resource 2 right away
synchronized(resource2) {
System.out.println("Thread 2: locked resource 2");
// Then it pauses, for the same reason as the first thread does
try { Thread.sleep(50); } catch (InterruptedException e) {}
// Then it tries to lock resource1. But wait! Thread 1 locked
// resource1, and won't release it 'till it gets a lock on
// resource2. This thread holds the lock on resource2, and won't
// release it 'till it gets resource1. We're at an impasse. Neither
// thread can run, and the program freezes up.
synchronized(resource1) {
System.out.println("Thread 2: locked resource 1");
}
}
}
};
// Start the two threads. If all goes as planned, deadlock will occur,
// and the program will never exit.
t1.start();
t2.start();
}
}

The producers-consumers problem together with the dining philosophers' problem is probably as simple as it's going to get. It has some pseudocode that illustrates it, as well. If those are too complex for a newbie they'd better try harder to grasp them.

Deadlock can occur in a situation when a Girl1 is wanting to flirt with Guy2, who is caught by another Girl2, and Girl2 is wanting to flirt with a Guy1 that is caught by Girl1. Since, both girls are waiting for dumping each other, the condition is called deadlock.
class OuchTheGirls
{
public static void main(String[] args)
{
final String resource1 = "Guy1";
final String resource2 = "Guy2";
// Girl1 tries to lock resource1 then resource2
Thread Girl1 = new Thread(() ->
{
synchronized (resource1)
{
System.out.println("Thread 1: locked Guy1");
try { Thread.sleep(100);} catch (Exception e) {}
synchronized (resource2)
{
System.out.println("Thread 1: locked Guy2");
}
}
});
// Girl2 tries to lock Guy2 then Guy1
Thread Girl2 = new Thread(() ->
{
synchronized (resource2)
{
System.out.println("Thread 2: locked Guy2");
try { Thread.sleep(100);} catch (Exception e) {}
synchronized (resource1)
{
System.out.println("Thread 2: locked Guy1");
}
}
});
Girl1.start();
Girl2.start();
}
}

Go for the simplist possible scenario in which deadlock can occur when introducting the concept to your students. This would involve a minimum of two threads and a minimum of two resources (I think). The goal being to engineer a scenario in which the first thread has a lock on resource one, and is waiting for the lock on resource two to be released, whilst at the same time thread two holds a lock on resource two, and is waiting for the lock on resource one to be released.
It doesn't really matter what the underlying resources are; for simplicities sake, you could just make them a pair of files that both threads are able to write to.
EDIT: This assumes no inter-process communication other than the locks held.

I found that a bit hard to understand when reading the dining philosophers' problem, deadlock IMHO is actually related to resource allocation.
Would like to share a more simple example where 2 Nurse need to fight for 3 equipment in order to complete a task.
Although it's written in java. A simple lock() method is created to simulate how the deadlock happen, so it can apply in other programming language as well.
http://www.justexample.com/wp/example-of-deadlock/

Simple example from https://docs.oracle.com/javase/tutorial/essential/concurrency/deadlock.html
public class Deadlock {
public static void printMessage(String message) {
System.out.println(String.format("%s %s ", Thread.currentThread().getName(), message));
}
private static class Friend {
private String name;
public Friend(String name) {
this.name = name;
}
public void bow(Friend friend) {
printMessage("Acquiring lock on " + this.name);
synchronized(this) {
printMessage("Acquired lock on " + this.name);
printMessage(name + " bows " + friend.name);
friend.bowBack(this);
}
}
public void bowBack(Friend friend) {
printMessage("Acquiring lock on " + this.name);
synchronized (this) {
printMessage("Acquired lock on " + this.name);
printMessage(friend.name + " bows back");
}
}
}
public static void main(String[] args) throws InterruptedException {
Friend one = new Friend("one");
Friend two = new Friend("two");
new Thread(new Runnable() {
#Override
public void run() {
one.bow(two);
}
}).start();
new Thread(new Runnable() {
#Override
public void run() {
two.bow(one);
}
}).start();
}
}
Output:
Thread-0 Acquiring lock on one
Thread-1 Acquiring lock on two
Thread-0 Acquired lock on one
Thread-1 Acquired lock on two
Thread-1 two bows one
Thread-0 one bows two
Thread-1 Acquiring lock on one
Thread-0 Acquiring lock on two
Thread Dump:
2016-03-14 12:20:09
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.74-b02 mixed mode):
"DestroyJavaVM" #13 prio=5 os_prio=0 tid=0x00007f472400a000 nid=0x3783 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Thread-1" #12 prio=5 os_prio=0 tid=0x00007f472420d800 nid=0x37a3 waiting for monitor entry [0x00007f46e89a5000]
java.lang.Thread.State: BLOCKED (on object monitor)
at com.anantha.algorithms.ThreadJoin$Friend.bowBack(ThreadJoin.java:102)
- waiting to lock <0x000000076d0583a0> (a com.anantha.algorithms.ThreadJoin$Friend)
at com.anantha.algorithms.ThreadJoin$Friend.bow(ThreadJoin.java:92)
- locked <0x000000076d0583e0> (a com.anantha.algorithms.ThreadJoin$Friend)
at com.anantha.algorithms.ThreadJoin$2.run(ThreadJoin.java:141)
at java.lang.Thread.run(Thread.java:745)
"Thread-0" #11 prio=5 os_prio=0 tid=0x00007f472420b800 nid=0x37a2 waiting for monitor entry [0x00007f46e8aa6000]
java.lang.Thread.State: BLOCKED (on object monitor)
at com.anantha.algorithms.ThreadJoin$Friend.bowBack(ThreadJoin.java:102)
- waiting to lock <0x000000076d0583e0> (a com.anantha.algorithms.ThreadJoin$Friend)
at com.anantha.algorithms.ThreadJoin$Friend.bow(ThreadJoin.java:92)
- locked <0x000000076d0583a0> (a com.anantha.algorithms.ThreadJoin$Friend)
at com.anantha.algorithms.ThreadJoin$1.run(ThreadJoin.java:134)
at java.lang.Thread.run(Thread.java:745)
"Monitor Ctrl-Break" #10 daemon prio=5 os_prio=0 tid=0x00007f4724211000 nid=0x37a1 runnable [0x00007f46e8def000]
java.lang.Thread.State: RUNNABLE
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
at java.net.SocketInputStream.read(SocketInputStream.java:170)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
- locked <0x000000076d20afb8> (a java.io.InputStreamReader)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.BufferedReader.fill(BufferedReader.java:161)
at java.io.BufferedReader.readLine(BufferedReader.java:324)
- locked <0x000000076d20afb8> (a java.io.InputStreamReader)
at java.io.BufferedReader.readLine(BufferedReader.java:389)
at com.intellij.rt.execution.application.AppMain$1.run(AppMain.java:93)
at java.lang.Thread.run(Thread.java:745)
"Service Thread" #9 daemon prio=9 os_prio=0 tid=0x00007f47240c9800 nid=0x3794 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C1 CompilerThread3" #8 daemon prio=9 os_prio=0 tid=0x00007f47240c6800 nid=0x3793 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread2" #7 daemon prio=9 os_prio=0 tid=0x00007f47240c4000 nid=0x3792 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread1" #6 daemon prio=9 os_prio=0 tid=0x00007f47240c2800 nid=0x3791 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread0" #5 daemon prio=9 os_prio=0 tid=0x00007f47240bf800 nid=0x3790 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Signal Dispatcher" #4 daemon prio=9 os_prio=0 tid=0x00007f47240be000 nid=0x378f waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Finalizer" #3 daemon prio=8 os_prio=0 tid=0x00007f472408c000 nid=0x378e in Object.wait() [0x00007f46e98c5000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x000000076cf88ee0> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143)
- locked <0x000000076cf88ee0> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:164)
at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:209)
"Reference Handler" #2 daemon prio=10 os_prio=0 tid=0x00007f4724087800 nid=0x378d in Object.wait() [0x00007f46e99c6000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x000000076cf86b50> (a java.lang.ref.Reference$Lock)
at java.lang.Object.wait(Object.java:502)
at java.lang.ref.Reference.tryHandlePending(Reference.java:191)
- locked <0x000000076cf86b50> (a java.lang.ref.Reference$Lock)
at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153)
"VM Thread" os_prio=0 tid=0x00007f4724080000 nid=0x378c runnable
"GC task thread#0 (ParallelGC)" os_prio=0 tid=0x00007f472401f000 nid=0x3784 runnable
"GC task thread#1 (ParallelGC)" os_prio=0 tid=0x00007f4724021000 nid=0x3785 runnable
"GC task thread#2 (ParallelGC)" os_prio=0 tid=0x00007f4724022800 nid=0x3786 runnable
"GC task thread#3 (ParallelGC)" os_prio=0 tid=0x00007f4724024800 nid=0x3787 runnable
"GC task thread#4 (ParallelGC)" os_prio=0 tid=0x00007f4724026000 nid=0x3788 runnable
"GC task thread#5 (ParallelGC)" os_prio=0 tid=0x00007f4724028000 nid=0x3789 runnable
"GC task thread#6 (ParallelGC)" os_prio=0 tid=0x00007f4724029800 nid=0x378a runnable
"GC task thread#7 (ParallelGC)" os_prio=0 tid=0x00007f472402b800 nid=0x378b runnable
"VM Periodic Task Thread" os_prio=0 tid=0x00007f47240cc800 nid=0x3795 waiting on condition
JNI global references: 16
Found one Java-level deadlock:
=============================
"Thread-1":
waiting to lock monitor 0x00007f46dc003f08 (object 0x000000076d0583a0, a com.anantha.algorithms.ThreadJoin$Friend),
which is held by "Thread-0"
"Thread-0":
waiting to lock monitor 0x00007f46dc006008 (object 0x000000076d0583e0, a com.anantha.algorithms.ThreadJoin$Friend),
which is held by "Thread-1"
Java stack information for the threads listed above:
===================================================
"Thread-1":
at com.anantha.algorithms.ThreadJoin$Friend.bowBack(ThreadJoin.java:102)
- waiting to lock <0x000000076d0583a0> (a com.anantha.algorithms.ThreadJoin$Friend)
at com.anantha.algorithms.ThreadJoin$Friend.bow(ThreadJoin.java:92)
- locked <0x000000076d0583e0> (a com.anantha.algorithms.ThreadJoin$Friend)
at com.anantha.algorithms.ThreadJoin$2.run(ThreadJoin.java:141)
at java.lang.Thread.run(Thread.java:745)
"Thread-0":
at com.anantha.algorithms.ThreadJoin$Friend.bowBack(ThreadJoin.java:102)
- waiting to lock <0x000000076d0583e0> (a com.anantha.algorithms.ThreadJoin$Friend)
at com.anantha.algorithms.ThreadJoin$Friend.bow(ThreadJoin.java:92)
- locked <0x000000076d0583a0> (a com.anantha.algorithms.ThreadJoin$Friend)
at com.anantha.algorithms.ThreadJoin$1.run(ThreadJoin.java:134)
at java.lang.Thread.run(Thread.java:745)
Found 1 deadlock.
Heap
PSYoungGen total 74752K, used 9032K [0x000000076cf80000, 0x0000000772280000, 0x00000007c0000000)
eden space 64512K, 14% used [0x000000076cf80000,0x000000076d8520e8,0x0000000770e80000)
from space 10240K, 0% used [0x0000000771880000,0x0000000771880000,0x0000000772280000)
to space 10240K, 0% used [0x0000000770e80000,0x0000000770e80000,0x0000000771880000)
ParOldGen total 171008K, used 0K [0x00000006c6e00000, 0x00000006d1500000, 0x000000076cf80000)
object space 171008K, 0% used [0x00000006c6e00000,0x00000006c6e00000,0x00000006d1500000)
Metaspace used 3183K, capacity 4500K, committed 4864K, reserved 1056768K
class space used 352K, capacity 388K, committed 512K, reserved 1048576K

Here's one simple deadlock in Java. We need two resources for demonstrating deadlock. In below example, one resource is class lock(via sync method) and the other one is an integer 'i'
public class DeadLock {
static int i;
static int k;
public static synchronized void m1(){
System.out.println(Thread.currentThread().getName()+" executing m1. Value of i="+i);
if(k>0){i++;}
while(i==0){
System.out.println(Thread.currentThread().getName()+" waiting in m1 for i to be > 0. Value of i="+i);
try { Thread.sleep(10000);} catch (InterruptedException e) { e.printStackTrace(); }
}
}
public static void main(String[] args) {
Thread t1 = new Thread("t1") {
public void run() {
m1();
}
};
Thread t2 = new Thread("t2") {
public void run() {
try { Thread.sleep(100);} catch (InterruptedException e) { e.printStackTrace(); }
k++;
m1();
}
};
t1.start();
t2.start();
}
}

public class DeadLock {
public static void main(String[] args) {
Object resource1 = new Object();
Object resource2 = new Object();
SharedObject s = new SharedObject(resource1, resource2);
TestThread11 t1 = new TestThread11(s);
TestThread22 t2 = new TestThread22(s);
t1.start();
t2.start();
}
}
class SharedObject {
Object o1, o2;
SharedObject(Object o1, Object o2) {
this.o1 = o1;
this.o2 = o2;
}
void m1() {
synchronized(o1) {
System.out.println("locked on o1 from m1()");
synchronized(o2) {
System.out.println("locked on o2 from m1()");
}
}
}
void m2() {
synchronized(o2) {
System.out.println("locked on o2 from m2()");
synchronized(o1) {
System.out.println("locked on o1 from m2()");
}
}
}
}
class TestThread11 extends Thread {
SharedObject s;
TestThread11(SharedObject s) {
this.s = s;
}
public void run() {
s.m1();
}
}
class TestThread22 extends Thread {
SharedObject s;
TestThread22(SharedObject s) {
this.s = s;
}
public void run() {
s.m2();
}
}

Here's a simple deadlock in C#.
void UpdateLabel(string text) {
lock(this) {
if(MyLabel.InvokeNeeded) {
IAsyncResult res = MyLable.BeginInvoke(delegate() {
MyLable.Text = text;
});
MyLabel.EndInvoke(res);
} else {
MyLable.Text = text;
}
}
}
If, one day, you call this from the GUI thread, and another thread calls it as well - you might deadlock. The other thread gets to EndInvoke, waits for the GUI thread to execute the
delegate while holding the lock. The GUI thread blocks on the same lock waiting for the other thread to release it - which it will not because the GUI thread will never be available to execute the delegate the other thread is waiting for. (ofcourse the lock here isn't strictly needed - nor is perhaps the EndInvoke, but in a slightly more complex scenario, a lock might be acquired by the caller for other reasons, resulting in the same deadlock.)

package test.concurrent;
public class DeadLockTest {
private static long sleepMillis;
private final Object lock1 = new Object();
private final Object lock2 = new Object();
public static void main(String[] args) {
sleepMillis = Long.parseLong(args[0]);
DeadLockTest test = new DeadLockTest();
test.doTest();
}
private void doTest() {
Thread t1 = new Thread(new Runnable() {
public void run() {
lock12();
}
});
Thread t2 = new Thread(new Runnable() {
public void run() {
lock21();
}
});
t1.start();
t2.start();
}
private void lock12() {
synchronized (lock1) {
sleep();
synchronized (lock2) {
sleep();
}
}
}
private void lock21() {
synchronized (lock2) {
sleep();
synchronized (lock1) {
sleep();
}
}
}
private void sleep() {
try {
Thread.sleep(sleepMillis);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
To run the deadlock test with sleep time 1 millisecond:
java -cp . test.concurrent.DeadLockTest 1

public class DeadlockProg {
/**
* #Gowtham Chitimi Reddy IIT(BHU);
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
final Object ob1 = new Object();
final Object ob2 = new Object();
Thread t1 = new Thread(){
public void run(){
synchronized(ob1){
try{
Thread.sleep(100);
}
catch(InterruptedException e){
System.out.println("Error catched");
}
synchronized(ob2){
}
}
}
};
Thread t2 = new Thread(){
public void run(){
synchronized(ob2){
try{
Thread.sleep(100);
}
catch(InterruptedException e){
System.out.println("Error catched");
}
synchronized(ob1){
}
}
}
};
t1.start();
t2.start();
}
}

package ForkBlur;
public class DeadLockTest {
public static void main(String args[]) {
final DeadLockTest t1 = new DeadLockTest();
final DeadLockTest t2 = new DeadLockTest();
Runnable r1 = new Runnable() {
#Override
public void run() {
try {
synchronized (t1) {
System.out
.println("r1 has locked t1, now going to sleep");
Thread.sleep(100);
System.out
.println("r1 has awake , now going to aquire lock for t2");
synchronized (t2) {
Thread.sleep(100);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
Runnable r2 = new Runnable() {
#Override
public void run() {
try {
synchronized (t2) {
System.out
.println("r2 has aquire the lock of t2 now going to sleep");
Thread.sleep(100);
System.out
.println("r2 is awake , now going to aquire the lock from t1");
synchronized (t1) {
Thread.sleep(100);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
new Thread(r1).start();
new Thread(r2).start();
}
}

I have created an ultra Simple Working DeadLock Example:-
package com.thread.deadlock;
public class ThreadDeadLockClient {
public static void main(String[] args) {
ThreadDeadLockObject1 threadDeadLockA = new ThreadDeadLockObject1("threadDeadLockA");
ThreadDeadLockObject2 threadDeadLockB = new ThreadDeadLockObject2("threadDeadLockB");
new Thread(new Runnable() {
#Override
public void run() {
threadDeadLockA.methodA(threadDeadLockB);
}
}).start();
new Thread(new Runnable() {
#Override
public void run() {
threadDeadLockB.methodB(threadDeadLockA);
}
}).start();
}
}
package com.thread.deadlock;
public class ThreadDeadLockObject1 {
private String name;
ThreadDeadLockObject1(String name){
this.name = name;
}
public synchronized void methodA(ThreadDeadLockObject2 threadDeadLockObject2) {
System.out.println("In MethodA "+" Current Object--> "+this.getName()+" Object passed as parameter--> "+threadDeadLockObject2.getName());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
threadDeadLockObject2.methodB(this);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.thread.deadlock;
public class ThreadDeadLockObject2 {
private String name;
ThreadDeadLockObject2(String name){
this.name = name;
}
public synchronized void methodB(ThreadDeadLockObject1 threadDeadLockObject1) {
System.out.println("In MethodB "+" Current Object--> "+this.getName()+" Object passed as parameter--> "+threadDeadLockObject1.getName());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
threadDeadLockObject1.methodA(this);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
In the above example 2 threads are executing the synchronized methods of two different objects.
Synchronized methodA is called by object threadDeadLockA and synchronized methodB is called by object threadDeadLockB. In methodA a reference of threadDeadLockB is passed and in methodB a reference of threadDeadLockA is passed. Now each thread tries to get lock on the another object. In methodA the thread who is holding a lock on threadDeadLockA is trying to get lock on object threadDeadLockB and similarly in methodB the thread who is holding a lock on threadDeadLockB is trying to get lock on threadDeadLockA. Thus both the threads will wait forever creating a deadlock.

Let me explain more clearly using an example having more than 2 threads.
Let us say you have n threads each holding locks L1, L2, ..., Ln respectively. Now let's say, starting from thread 1, each thread tries to acquire its neighbour thread's lock. So, thread 1 gets blocked for trying to acquire L2 (as L2 is owned by thread 2), thread 2 gets blocked for L3 and so on. The thread n gets blocked for L1. This is now a deadlock as no thread is able to execute.
class ImportantWork{
synchronized void callAnother(){
}
synchronized void call(ImportantWork work) throws InterruptedException{
Thread.sleep(100);
work.callAnother();
}
}
class Task implements Runnable{
ImportantWork myWork, otherWork;
public void run(){
try {
myWork.call(otherWork);
} catch (InterruptedException e) {
}
}
}
class DeadlockTest{
public static void main(String args[]){
ImportantWork work1=new ImportantWork();
ImportantWork work2=new ImportantWork();
ImportantWork work3=new ImportantWork();
Task task1=new Task();
task1.myWork=work1;
task1.otherWork=work2;
Task task2=new Task();
task2.myWork=work2;
task2.otherWork=work3;
Task task3=new Task();
task3.myWork=work3;
task3.otherWork=work1;
new Thread(task1).start();
new Thread(task2).start();
new Thread(task3).start();
}
}
In the above example, you can see that there are three threads holding Runnables task1, task2, and task3. Before the statement sleep(100) the threads acquire the three work objects' locks when they enter the call() method (due to the presence of synchronized). But as soon as they try to callAnother() on their neighbour thread's object, they are blocked, leading to a deadlock, because those objects' locks have already been taken.

CountDownLatch countDownLatch = new CountDownLatch(1);
ExecutorService executorService = ExecutorService executorService = Executors.newSingleThreadExecutor();
executorService.execute(() -> {
Future<?> future = executorService.submit(() -> {
System.out.println("generated task");
});
countDownLatch.countDown();
try {
future.get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
});
countDownLatch.await();
executorService.shutdown();

A sneaky way to deadlock with just a single thread is to try to lock the same (non-recursive) mutex twice. This might not be the simple example you were looking for, but sure enough I encountered such cases already.
#include <mutex>
#include <iostream>
int main()
{
std::mutex m;
m.lock();
m.lock();
std::cout << "Expect never to get here because of a deadlock!";
}

Here is my detailed example for deadlock, after spending lots of time. Hope it helps :)
package deadlock;
public class DeadlockApp {
String s1 = "hello";
String s2 = "world";
Thread th1 = new Thread() {
public void run() {
System.out.println("Thread th1 has started");
synchronized (s1) { //A lock is created internally (holds access of s1), lock will be released or unlocked for s1, only when it exits the block Line #23
System.out.println("Executing first synchronized block of th1!");
try {
Thread.sleep(1000);
} catch(InterruptedException ex) {
System.out.println("Exception is caught in th1");
}
System.out.println("Waiting for the lock to be released from parrallel thread th1");
synchronized (s2) { //As another has runned parallely Line #32, lock has been created for s2
System.out.println(s1 + s2);
}
}
System.out.println("Thread th1 has executed");
}
};
Thread th2 = new Thread() {
public void run() {
System.out.println("Thread th2 has started");
synchronized (s2) { //A lock is created internally (holds access of s2), lock will be released or unlocked for s2, only when it exits the block Line #44
System.out.println("Executing first synchronized block of th2!");
try {
Thread.sleep(1000);
} catch(InterruptedException ex) {
System.out.println("Exception is caught in th2");
}
System.out.println("Waiting for the lock to be released from parrallel thread th2");
synchronized (s1) { //As another has runned parallely Line #11, lock has been created for s1
System.out.println(s1 + s2);
}
}
System.out.println("Thread th2 has executed");
}
};
public static void main(String[] args) {
DeadlockApp deadLock = new DeadlockApp();
deadLock.th1.start();
deadLock.th2.start();
//Line #51 and #52 runs parallely on executing the program, a lock is created inside synchronized method
//A lock is nothing but, something like a blocker or wall, which holds access of the variable from being used by others.
//Locked object is accessible, only when it is unlocked (i.e exiting the synchronized block)
//Lock cannot be created for primitive types (ex: int, float, double)
//Dont forget to add thread.sleep(time) because if not added, then object access will not be at same time for both threads to create Deadlock (not actual runtime with lots of threads)
//This is a simple program, so we added sleep90 to create Deadlock, it will execute successfully, if it is removed.
}
//Happy coding -- Parthasarathy S
}

Related

Execution of a thread after it got interrupted

A thread is executing task to print numbers from 0 to n, and sleeps for 4000 ms after every print statement. Somewhere in the middle thread gets interrupted. Now when the same thread starts its execution, where will it start from , will it start printing the numbers from 0 to n again, or it will print numbers from where it got interrupted.
In both cases what are the reasons and how it is being handled?
public class Main {
public static void main(String[] args) throws InterruptedException {
SleepTest sleepTest = new SleepTest();
Thread thread = new Thread(sleepTest);
thread.start();
thread.interrupt();
}
}
public class SleepTest implements Runnable{
static int sleep = 10;
public void run(){
for (int i =0; i<10; i++){
System.out.println(i);
try {
Thread.currentThread().interrupt();
Thread.sleep(4000);
} catch (InterruptedException exception) {
exception.printStackTrace();
}
System.out.println(Thread.interrupted());
}
}
Calling a interrupt() on a thread object can only suggest thread to stop. It is not guarantee that the thread will stop.
It completely depends on the implementation of run() method of thread.
In your case in run() you are catching the InterruptedException and you are printing the exception trace but not stopping the thread. Thats why thread will never stop on InterruptedException and continue the execution.
It may look like thread is getting stopped(by seeing exception trace) when see the output on console.
Refer interrupt interrupted isinterrupted in Java
All Thread.currentThread().interrupt() does is update the value of field interrupted to true.
Let's see the program's flow and how the interrupted field is assigned values:
public class Main {
public static void main(String[] args) throws InterruptedException {
SleepTest sleepTest = new SleepTest();
Thread thread = new Thread(sleepTest, "Sub Thread"); // Give a name to this thread
thread.start(); // main thread spawns the "Sub Thread". Value of "interrupted" - false
thread.interrupt(); // called by main thread. Value of "interrupted" - true
}
}
public class SleepTest implements Runnable{
static int sleep = 10;
public void run(){
System.out.println(Thread.currentThread().getName()+" "+Thread.interrupted()); // prints "Sub Thread true"
for (int i =0; i<10; i++){
System.out.println(i);
try {
Thread.currentThread().interrupt(); // no matter what value is for interrupted, it is assigned the value "true"
Thread.sleep(4000); // Can't call sleep with a "true" interrupted status. Exception is thrown. Note that, when the exception is thrown, the value of interrupted is "reset", i.e., set to false
} catch (InterruptedException exception) {
exception.printStackTrace(); // whatever
}
System.out.println(Thread.interrupted()); // returns the value of interrupted and resets it to false
}
}
To answer
where will it start from , will it start printing the numbers from 0
to n again, or it will print numbers from where it got interrupted.
Calling interrupt will not cause make it start over because all it is doing at this call is set value interrupted to false (and not modifying anything else).

Multithreading: Same two object is entering into synchronized block

May be my header would not be correct.
I have started java multithread concept with programming. since i have read inside synchronized block only one thread will inter on a particular object lock. But i have confused after looking the output of this program.
package com.example.classandobjectlevellock;
class MyThread implements Runnable
{
Object ob = new Object();
public void run() {
synchronized (this) {
System.out.println(Thread.currentThread().getName()+" Is waitng");
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class ClassAndObjectLevelLock {
public static void main(String[] args) throws InterruptedException {
MyThread task1 = new MyThread();
MyThread task2 = new MyThread();
Thread t1 = new Thread(task1,"Thread1");
Thread t2 = new Thread(task1,"Thread2");
Thread t3 = new Thread(task2,"Thread3");
t1.start();
Thread.sleep(1000);
t2.start();
Thread.sleep(1000);
t3.start();
}
}
Output:
Thread1 Is waitng
Thread2 Is waitng
Thread3 Is waitng
If i am not wrong, Thread-1 and thread-3 is entering into synchronized method because it having two different target object. But why Thread-2 is entering into Synchronized block?
Please help me for understanding of this .
Thanks in advance.
Calling wait() causes the lock to be released.
Per the wait() Javadocs:
Causes the current thread to wait until another thread invokes the
notify() method or the notifyAll() method for this object. In
other words, this method behaves exactly as if it simply performs the
call wait(0).
The current thread must own this object's monitor. The thread
releases ownership of this monitor and waits until another thread
notifies threads waiting on this object's monitor to wake up either
through a call to the notify method or the notifyAll method. The
thread then waits until it can re-obtain ownership of the monitor and
resumes execution.

Why is that Thread interupt method is not breaking its sleep method?

I have this program below
package com;
public class ThreadDemo implements Runnable {
#Override
public void run() {
while(true)
{
try {
System.out.println("Into sleep");
Thread.sleep(1000000000);
System.out.println("Out of sleep");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String args[]) {
ThreadDemo tD = new ThreadDemo();
Thread t1 = new Thread(tD);
t1.start();
t1.interrupt();
}
}
I have started the Thread , and will call its run method and goes into sleep state for the above specified seconds .
i have called t1.interrupt();
This is the screen shot
My question is that why
1.The Thread is not coming out of sleep state ?
Why Into sleep is printed twice ??
You're in a loop:
You're sleeping
Being interrupted and printing the stack trace
Going back to sleep
Never being interrupted again
So it is "coming out of sleep state" (otherwise you wouldn't see the stack trace) - but you're then calling Thread.sleep again.
If you only want to sleep once, get rid of the loop...
1.The Thread is not comng out of sleep state ?
He actually is but
System.out.println("Out of sleep");
is never executed because when you interrupt Thread.sleep(10000); throws a exception and
e.printStackTrace();
is execute instead

Threading in Spring

I'm trying to do some optimization in my code and would like to spawn a thread where I do a time consuming operation. During the implementation of that optimization I was running into an issue which was driving me crazy. I simplified the issue and created a test case for that specific issue: (I'm using SpringJUnit4ClassRunner so the transaction is properly started at the beginning of the testCRUD method)
Could someone help me understand why the foundParent is null in the thread ?
private Semaphore sema = new Semaphore(0, false);
private long parentId;
#Test
public void testCRUD() {
//create
DBParent parent = null;
{
parent = new DBParent();
parentDao.persist(parent);
parentId = parent.getId();
assertTrue(parentId > 0);
parentDao.flush();
}
(new Thread(
new Runnable() {
public void run()
{
System.out.println("Start adding childs !");
DBParent foundParent = parentDao.findById(parentId);
assertTrue(foundParent != null); //ASSERTION FAILS HERE !!!!
System.out.println("Releasing semaphore !");
sema.release();
System.out.println("End adding childs !");
}
})).start();
try {
System.out.println("Acquiring semaphore !");
sema.acquire();
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
=============================EDITED===================================
As per one comment suggestion, I created a threadManager bean which spawn the thread. Here is the code of the threadManager:
public class ThreadManager {
#Transactional(propagation=Propagation.REQUIRES_NEW)
public void executeTask(String Name, Runnable task) {
(new Thread(task, Name)).start();
}
}
Then in the previous test, instead of staring the thread manually, I just post it in the thread manager like this:
#Autowired private ParentDao parentDao;
#Autowired private ThreadManager threadManager;
private Semaphore sema = new Semaphore(0, false);
private long parentId;
#Test
public void testCRUD() {
//create
DBParent parent = null;
{
parent = new DBParent();
parentDao.persist(parent);
parentId = parent.getId();
assertTrue(parentId > 0);
parentDao.flush();
}
threadManager.executeTask("BG processing...",
new Runnable() {
public void run()
{
System.out.println("Start adding childs !");
DBParent foundParent = parentDao.findById(parentId);
assertTrue(foundParent != null); //ASSERTION FAILS HERE !!!!
System.out.println("Releasing semaphore !");
sema.release();
System.out.println("End adding childs !");
}
});
try {
System.out.println("Acquiring semaphore !");
sema.acquire();
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
Unfortunately this doesn't work either !!! :-(
The transaction context is bound to the thread. So the code in the spawned thread doesn't run in the same transaction context as the code in the initial thread. So, due to transaction isolation (the I in ACID), the spawned thread doesn't see what the initial thread's transaction is inserting in the database.
You can bind Spring transaction to a new thread, to run transactions & Hibernate/JPA access in it. But this has to be a different TX and JPA/HB session from other threads.
Spring code for OpenSessionInViewFilter, is a reasonable an example of how to bind Hibernate session to Spring's TX management. You can strip this down to fairly minimal code.
See:
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
OpenSessionInViewFilter.doFilterInternal() -- this is where it actually binds it
TransactionSynchronizationManager.bindResource()
TransactionSynchronizationManager.unbindResource()
TransactionSynchronizationManager.getResource()
In one project (IIRC) I wrapped this functionality into a 'ServerThreadHb' class, to setup & save previous thread-bindings on construction -- with a restore() method to be called in a finally block, to restore previous bindings.
For your posted code sample, there isn't much point in running work on a separate thread -- since you synchronously wait for the work to be done. However I assume you were planning to remove that constraint & extend that functionality.

System.exit is not thread-safe on Linux?

I've just switched from Oracle JDK 1.6, to Open JDK 1.7.0_03, and I've hit a rather remarkable deadlock on exit:
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
at java.lang.Thread.join(Thread.java:1258)
- locked <0x8608dda0> (a sun.awt.X11.XToolkit$1$1)
at java.lang.Thread.join(Thread.java:1332)
at java.lang.ApplicationShutdownHooks.runHooks(ApplicationShutdownHooks.java:106)
at java.lang.ApplicationShutdownHooks$1.run(ApplicationShutdownHooks.java:46)
at java.lang.Shutdown.runHooks(Shutdown.java:123)
at java.lang.Shutdown.sequence(Shutdown.java:167)
at java.lang.Shutdown.exit(Shutdown.java:212)
- locked <0x8603df28> (a java.lang.Class for java.lang.Shutdown)
at java.lang.Runtime.exit(Runtime.java:107)
at java.lang.System.exit(System.java:960)
It appears that you must call System.exit from the AWT event queue. Is this for real? There is no documentation of a thread requirement in the Sun docs Runtime.exit
I've hit other surprising cases where getting the AWT tree lock is required only on Linux, but this one takes the cake. Is this a bug, or have I just missed something in the documentation?
It depends, the runHooks method will start any hook threads registered through Runtime.addShutdownHook and wait for them to be finished. If any of your hook threads is locking some resources that the AWT event thread is requiring too, they may cause dead lock.
If you have to call System.exit in your AWT event thread,I suggest you call it in another thread like:
new Thread(){
public void run() {
System.exit(0);
}
}.start();
It is impossible to say whether this is a bug in the runtime without knowing more about what the application is doing (ideally, this would take the form of an SSCCE).
For example, the following demonstrates a similar deadlock involving System.exit(). However, it is clearly a bug in the application, not in System.exit():
public class OhNo {
final static Object lock = new Object();
public static void main(String[] args) {
new Thread(new Runnable() {
public void run() {
synchronized (lock) {
for (;;) {
}
}
}
}).start();
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
public void run() {
synchronized (lock) {
System.out.println("in shutdown hook");
}
}
}));
System.out.println("about to call System.exit()");
System.exit(0);
}
}

Resources