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); } }