0

I'm trying to learn java multithreading . When it comes to deadlock it took me really hard time to understand the concepts . This is my own coding to understand deadlock.But it compiling without an error.But when i run it shows a Nullpointer Exception .And here is the Error

Stacktrace:

Exception in thread "Thread-0" java.lang.NullPointerException at deadlockA.run(deadlock.java:70) at java.lang.Thread.run(Thread.java:745) Exception in thread "Thread-1" java.lang.NullPointerException at deadlockB.run(deadlock.java:91) at java.lang.Thread.run(Thread.java:745) 

Please help me to get the clear picture of error.

Code:

import java.io.*; class A { public synchronized void funcA(B b) { System.out.println("INSIDE FIRST OBJECTS MONITOR"); try { Thread.sleep(1000); } catch (InterruptedException e) { System.out.println(e); } b.last(); } public synchronized void last() { System.out.println("INSIDE A's LAST"); } } class B { public synchronized void funcB(A a) { System.out.println("INSIDE SECOND OBJECT MONITOR"); try { Thread.sleep(1000); } catch (InterruptedException e) { System.out.println(e); } a.last(); } public synchronized void last() { System.out.println("INSIDE A's LAST"); } } class deadlockA implements Runnable { Thread t; A a1; B b1; deadlockA(B b2) { b1 = b2; t = new Thread(this); t.start(); } public void run() { a1.funcA(b1); } } class deadlockB implements Runnable { Thread t; A a2; B b3; deadlockB(A a3) { a2 = a3; t = new Thread(this); t.start(); } public void run() { b3.funcB(a2); } } class deadlock { public static void main(String args[]) { A A1 = new A(); B B1 = new B(); deadlockA da = new deadlockA(B1); deadlockB db = new deadlockB(A1); } } 
0

4 Answers 4

2

The objects a1 and b3 are never initialized,hence calling a function on them gives a NullPointerException. Check the corrected code below:

class deadlockA implements Runnable { Thread t; A a1; B b1; deadlockA(B b2) { a1 = new A(); //Create an object b1=b2; t=new Thread(this); t.start(); } public void run() { a1.funcA(b1); } } class deadlockB implements Runnable { Thread t; A a2; B b3; deadlockB(A a3) { b3 = new B(); // Create an object a2=a3; t=new Thread(this); t.start(); } public void run() { b3.funcB(a2); } } 

And if you want to actually see a deadlock scenario,consider looking into the below posted code :

public class MyDeadlock { String str1 = "Java"; String str2 = "CPP"; Thread trd1 = new Thread("My Thread 1"){ public void run(){ while(true){ synchronized(str1){ synchronized(str2){ System.out.println(str1 + str2); } } } } }; Thread trd2 = new Thread("My Thread 2"){ public void run(){ while(true){ synchronized(str2){ synchronized(str1){ System.out.println(str2 + str1); } } } } }; public static void main(String a[]){ MyDeadlock mdl = new MyDeadlock(); mdl.trd1.start(); mdl.trd2.start(); } } 
Sign up to request clarification or add additional context in comments.

3 Comments

.I have a successful execution of this program. But there is not any sign of Deadlock .if its a deadlock then why doesn't it hang? Does this program clearly or atleast partially mirrors a deadlock ? . I'm totally confused.
Look at the recent edit posted by me,it demonstrates how deadlock occurs
Yeah now i understood.
2

In deadlockA(B b2) constructor you don't initialize a1 class variable but use it in run(). Same for b3 in deadlockB(A a3).

1 Comment

I have a successful execution of this program. But there is not any sign of Deadlock .if its a deadlock then why doesn't it hang? Does this program clearly or atleast partially mirrors a deadlock ? . I'm totally confused.
1

Your deadlockA class does not initialize a1. And that's why the program throws NullPointerException. You have it in your stack trace:

Exception in thread "Thread-0" java.lang.NullPointerException //what was thrown at deadlockA.run(deadlockz.java:70) //where it was throw - method 'run' of class 'deadlockA', line 70 of a file where you have deadlockA defined. 

Your problematic class with comments:

class deadlockA implements Runnable { Thread t; A a1; //not initialized. B b1; deadlockA(B b2) { b1=b2; t=new Thread(this); t.start(); } public void run() { a1.funcA(b1); //a1 object is null. } } 

1 Comment

Schielmann . I have a successful execution of this program. But there is not any sign of Deadlock .if its a deadlock then why doesn't it hang? Does this program clearly or atleast partially mirrors a deadlock ? . I'm totally confused.
1

The variable a1 and b3 is not initialized anywhere and that causes the NullPointerException. BTW the stack trace that you have posted is not relevant to the code that you have posted.

The code below will help you understand the deadlock situation better. There are two objects, thread1 acquires lock on object1 and thread2 acquires lock on object2 and after 5 seconds thread1 is attempting to acquire lock on object2 which is held by thread2 and thread2 is attempting to acquire lock on object1 which is held by thread1 and here comes the circular dependency (Deadlock). Hope this helps.

final Object object1 = new Object(); final Object object2 = new Object(); Thread thread1 = new Thread() { public void run() { synchronized (object1) { try { Thread.sleep(5 * 1000); synchronized (object2) { System.out.println("I am here"); } } catch (InterruptedException e) { e.printStackTrace(); } } } }; Thread thread2 = new Thread() { public void run() { synchronized (object2) { try { Thread.sleep(5 * 1000); synchronized (object1) { System.out.println("I am here"); } } catch (InterruptedException e) { e.printStackTrace(); } } } }; thread1.start(); thread2.start(); 

6 Comments

May I know why do you think it isn't relevant?
The stack trace says deadlockz.java but there is no such class name in the code that you have posted (at deadlockA.run(deadlockz.java:70))
The class in which the main is present,is not public,hence the filename can be anything.
@LoganathanMohanraj . Yeah .that's my fault. Corrected it.
@LoganathanMohanraj . If the code is perfectly a deadlock , then why doesn't it hang.?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.