Searching...
Tuesday, 23 April 2013

How to create Deadlocks using Java thread

08:15

DeadLock

Deadlock describes a situation where two or more threads are blocked forever, waiting for each other
Here is one simple example to create dead lock and remove deadlock using Java threads

package com.vinod.test;

import java.util.ArrayList;
import java.util.List;

/**
 * In this program two threads are trying to read students and teachers details.
 *
 *@authorvinod
 *
 */
public class SimpleDeadLock {
public static void main(String[] args) {
final List<String> students = new ArrayList<String>();
students.add("Raju");
students.add("Radha");

final List<String> teachers = new ArrayList<String>();
teachers.add("Gopal");
teachers.add("Ragav");

Thread t1 = new Thread() {
public void run() {
synchronized (students) {
System.out.println("Now thread 1 locked student arraylist");
System.out.println("students details from thread1" + students);

try {
Thread.sleep(110);
} catch (InterruptedException e) {
}

synchronized (teachers) {
System.out.println("Now thread 1 locked teacher arraylist");
System.out.println("teacher details from thread1" + teachers);
}
}
}
};

Thread t2 = new Thread() {
public void run() {
synchronized (teachers) {
System.out.println("Now thread 2 locked teacher arraylist");
System.out.println("teacher details from thread 2" + teachers);
try {
Thread.sleep(110);
} catch (InterruptedException e) {
}

synchronized (students) {
System.out.println("Now thread 2 locked student arraylist");
System.out.println("students details from thread2" + students);
}
}
}
};

t1.start();
t2.start();
}
}

 

Output

Now thread 2 locked teacher arraylist
Now thread 1 locked student arraylist
students details from thread1[Raju, Radha]
teacher details from thread 2[Gopal, Ragav]

How to remove this deadlock?

This above example will never end and each thread is waiting for the resources mutually. Here i am changing the order of synchronization block and remove the dead lock

package com.vinod.test;

import java.util.ArrayList;
import java.util.List;

/**
 * In this program two threads are trying to read students and teachers details.
 *
 *@authorvinod
 *
 */
public class SimpleDeadLock {
public static void main(String[] args) {

final List<String> students = new ArrayList<String>();
students.add("Raju");
students.add("Radha");

final List<String> teachers = new ArrayList<String>();
teachers.add("Gopal");
teachers.add("Ragav");

Thread t1 = new Thread() {
public void run() {
synchronized (students) {
System.out.println("Now thread 1 locked student arraylist");
System.out.println("students details from thread1" + students);

try {
Thread.sleep(110);
} catch (InterruptedException e) {
}

synchronized (teachers) {
System.out.println("Now thread 1 locked teacher arraylist");
System.out.println("teacher details from thread1" + teachers);
}
}
}
};

Thread t2 = new Thread() {
public void run() {
synchronized (students) {
System.out.println("Now thread 2 locked student arraylist");
System.out.println("students details from thread2" + students);
}
synchronized (teachers) {
System.out.println("Now thread 2 locked teacher arraylist");
System.out.println("teacher details from thread 2" + teachers);
try {
Thread.sleep(110);
} catch (InterruptedException e) {
}

}
}
};

t1.start();
t2.start();
}
}

Output

Now thread 1 locked student arraylist
students details from thread1[Raju, Radha]
Now thread 1 locked teacher arraylist
teacher details from thread1[Gopal, Ragav]
Now thread 2 locked student arraylist
students details from thread2[Raju, Radha]
Now thread 2 locked teacher arraylist
teacher details from thread 2[Gopal, Ragav]

0 comments: