How Can We Create a Deadlock on a Servlet

Deadlock in coffee is a programming situation where two or more threads are blocked forever. Coffee deadlock state of affairs arises with at least two threads and 2 or more resources. Hither I have written a uncomplicated programme that will cause java deadlock scenario and then we will see how to clarify information technology.

Deadlock in Coffee

deadlock in java, java thread deadlock

Let's accept a look at a unproblematic program where I will create deadlock in java threads.

                                  package com.journaldev.threads;  public course ThreadDeadlock {      public static void main(String[] args) throws InterruptedException {         Object obj1 = new Object();         Object obj2 = new Object();         Object obj3 = new Object();              Thread t1 = new Thread(new SyncThread(obj1, obj2), "t1");         Thread t2 = new Thread(new SyncThread(obj2, obj3), "t2");         Thread t3 = new Thread(new SyncThread(obj3, obj1), "t3");                  t1.get-go();         Thread.sleep(5000);         t2.start();         Thread.slumber(5000);         t3.start();              }  }  class SyncThread implements Runnable{     private Object obj1;     private Object obj2;      public SyncThread(Object o1, Object o2){         this.obj1=o1;         this.obj2=o2;     }     @Override     public void run() {         String name = Thread.currentThread().getName();         System.out.println(name + " acquiring lock on "+obj1);         synchronized (obj1) {          System.out.println(name + " acquired lock on "+obj1);          piece of work();          System.out.println(name + " acquiring lock on "+obj2);          synchronized (obj2) {             System.out.println(name + " acquired lock on "+obj2);             piece of work();         }          Arrangement.out.println(name + " released lock on "+obj2);         }         System.out.println(proper noun + " released lock on "+obj1);         Organization.out.println(name + " finished execution.");     }     individual void piece of work() {         attempt {             Thread.sleep(30000);         } catch (InterruptedException e) {             e.printStackTrace();         }     } }                              

In above programme SyncThread is implementing Runnable interface and it works on 2 Objects by acquiring lock on each one of them i past one using synchronized cake.

In main method, I have three threads running for SyncThread and at that place is a shared resources between each of the threads. The threads are run in such a manner that it will be able to acquire lock on the start object only when it'south trying to acquire lock on second object, it goes on look state because information technology'south already locked by another thread. This forms a cyclic dependency for resources betwixt Threads causing deadlock.

When I execute the to a higher place program, hither is the output generated but program never terminates because of deadlock in java threads.

                                  t1 acquiring lock on java.lang.Object@6d9dd520 t1 acquired lock on java.lang.Object@6d9dd520 t2 acquiring lock on java.lang.Object@22aed3a5 t2 caused lock on java.lang.Object@22aed3a5 t3 acquiring lock on java.lang.Object@218c2661 t3 acquired lock on coffee.lang.Object@218c2661 t1 acquiring lock on coffee.lang.Object@22aed3a5 t2 acquiring lock on java.lang.Object@218c2661 t3 acquiring lock on coffee.lang.Object@6d9dd520                              

Hither we can clearly identify the deadlock situation from the output but in existent life applications information technology's very hard to find the deadlock situation and debug them.

How to Detect Deadlock in Java

To discover a deadlock in coffee, we need to look at the java thread dump of the application, in last mail service I explained how nosotros tin can generate thread dump using VisualVM profiler or using jstack utility.

Here is the thread dump of above programme.

                                  2012-12-27 nineteen:08:34 Full thread dump Java HotSpot(TM) 64-Bit Server VM (23.5-b02 mixed way):  "Adhere Listener" daemon prio=five tid=0x00007fb0a2814000 nid=0x4007 waiting on condition [0x0000000000000000]    java.lang.Thread.Country: RUNNABLE  "DestroyJavaVM" prio=5 tid=0x00007fb0a2801000 nid=0x1703 waiting on condition [0x0000000000000000]    java.lang.Thread.State: RUNNABLE  "t3" prio=v tid=0x00007fb0a204b000 nid=0x4d07 waiting for monitor entry [0x000000015d971000]    java.lang.Thread.Land: BLOCKED (on object monitor) 	at com.journaldev.threads.SyncThread.run(ThreadDeadlock.java:41) 	- waiting to lock <0x000000013df2f658> (a coffee.lang.Object) 	- locked <0x000000013df2f678> (a java.lang.Object) 	at java.lang.Thread.run(Thread.java:722)  "t2" prio=5 tid=0x00007fb0a1073000 nid=0x4207 waiting for monitor entry [0x000000015d209000]    java.lang.Thread.Country: BLOCKED (on object monitor) 	at com.journaldev.threads.SyncThread.run(ThreadDeadlock.java:41) 	- waiting to lock <0x000000013df2f678> (a java.lang.Object) 	- locked <0x000000013df2f668> (a coffee.lang.Object) 	at coffee.lang.Thread.run(Thread.java:722)  "t1" prio=five tid=0x00007fb0a1072000 nid=0x5503 waiting for monitor entry [0x000000015d86e000]    java.lang.Thread.Country: BLOCKED (on object monitor) 	at com.journaldev.threads.SyncThread.run(ThreadDeadlock.coffee:41) 	- waiting to lock <0x000000013df2f668> (a java.lang.Object) 	- locked <0x000000013df2f658> (a java.lang.Object) 	at coffee.lang.Thread.run(Thread.coffee:722)  "Service Thread" daemon prio=5 tid=0x00007fb0a1038000 nid=0x5303 runnable [0x0000000000000000]    java.lang.Thread.Land: RUNNABLE  "C2 CompilerThread1" daemon prio=5 tid=0x00007fb0a1037000 nid=0x5203 waiting on status [0x0000000000000000]    java.lang.Thread.Country: RUNNABLE  "C2 CompilerThread0" daemon prio=5 tid=0x00007fb0a1016000 nid=0x5103 waiting on condition [0x0000000000000000]    java.lang.Thread.State: RUNNABLE  "Signal Dispatcher" daemon prio=5 tid=0x00007fb0a4003000 nid=0x5003 runnable [0x0000000000000000]    coffee.lang.Thread.State: RUNNABLE  "Finalizer" daemon prio=five tid=0x00007fb0a4800000 nid=0x3f03 in Object.wait() [0x000000015d0c0000]    java.lang.Thread.State: WAITING (on object monitor) 	at java.lang.Object.wait(Native Method) 	- waiting on <0x000000013de75798> (a java.lang.ref.ReferenceQueue$Lock) 	at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.coffee:135) 	- locked <0x000000013de75798> (a java.lang.ref.ReferenceQueue$Lock) 	at coffee.lang.ref.ReferenceQueue.remove(ReferenceQueue.coffee:151) 	at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:177)  "Reference Handler" daemon prio=5 tid=0x00007fb0a4002000 nid=0x3e03 in Object.await() [0x000000015cfbd000]    java.lang.Thread.State: WAITING (on object monitor) 	at java.lang.Object.await(Native Method) 	- waiting on <0x000000013de75320> (a java.lang.ref.Reference$Lock) 	at java.lang.Object.wait(Object.java:503) 	at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:133) 	- locked <0x000000013de75320> (a java.lang.ref.Reference$Lock)  "VM Thread" prio=v tid=0x00007fb0a2049800 nid=0x3d03 runnable   "GC task thread#0 (ParallelGC)" prio=five tid=0x00007fb0a300d800 nid=0x3503 runnable   "GC job thread#one (ParallelGC)" prio=v tid=0x00007fb0a2001800 nid=0x3603 runnable   "GC task thread#2 (ParallelGC)" prio=5 tid=0x00007fb0a2003800 nid=0x3703 runnable   "GC chore thread#3 (ParallelGC)" prio=5 tid=0x00007fb0a2004000 nid=0x3803 runnable   "GC task thread#4 (ParallelGC)" prio=5 tid=0x00007fb0a2005000 nid=0x3903 runnable   "GC task thread#5 (ParallelGC)" prio=five tid=0x00007fb0a2005800 nid=0x3a03 runnable   "GC task thread#6 (ParallelGC)" prio=5 tid=0x00007fb0a2006000 nid=0x3b03 runnable   "GC task thread#7 (ParallelGC)" prio=five tid=0x00007fb0a2006800 nid=0x3c03 runnable   "VM Periodic Task Thread" prio=5 tid=0x00007fb0a1015000 nid=0x5403 waiting on condition   JNI global references: 114   Found ane Java-level deadlock: ============================= "t3":   waiting to lock monitor 0x00007fb0a1074b08 (object 0x000000013df2f658, a java.lang.Object),   which is held past "t1" "t1":   waiting to lock monitor 0x00007fb0a1010f08 (object 0x000000013df2f668, a java.lang.Object),   which is held by "t2" "t2":   waiting to lock monitor 0x00007fb0a1012360 (object 0x000000013df2f678, a java.lang.Object),   which is held by "t3"  Java stack information for the threads listed higher up: =================================================== "t3": 	at com.journaldev.threads.SyncThread.run(ThreadDeadlock.java:41) 	- waiting to lock <0x000000013df2f658> (a java.lang.Object) 	- locked <0x000000013df2f678> (a java.lang.Object) 	at java.lang.Thread.run(Thread.java:722) "t1": 	at com.journaldev.threads.SyncThread.run(ThreadDeadlock.java:41) 	- waiting to lock <0x000000013df2f668> (a java.lang.Object) 	- locked <0x000000013df2f658> (a java.lang.Object) 	at coffee.lang.Thread.run(Thread.coffee:722) "t2": 	at com.journaldev.threads.SyncThread.run(ThreadDeadlock.java:41) 	- waiting to lock <0x000000013df2f678> (a coffee.lang.Object) 	- locked <0x000000013df2f668> (a java.lang.Object) 	at coffee.lang.Thread.run(Thread.java:722)  Found ane deadlock.                              

The thread dump output clearly shows the deadlock situation and threads and resource involved causing deadlock state of affairs.

For analyzing deadlock, nosotros need to look out for the threads with state as BLOCKED and then the resources it's waiting to lock. Every resource has a unique ID using which we can observe which thread is already holding the lock on the object. For example Thread "t3" is waiting to lock 0x000000013df2f658 but it'south already locked by thread "t1".

In one case we analyze the deadlock state of affairs and found out the threads which are causing deadlock, we demand to make code changes to avoid deadlock situation.

How to avoid deadlock in java

These are some of the guidelines using which nosotros tin can avoid most of the deadlock situations.

  • Avoid Nested Locks: This is the nearly common reason for deadlocks, avert locking some other resources if y'all already hold one. Information technology'south almost incommunicable to go deadlock situation if you are working with only one object lock. For instance, hither is the another implementation of run() method without nested lock and program runs successfully without deadlock state of affairs.
                                              public void run() {         String proper noun = Thread.currentThread().getName();         System.out.println(proper name + " acquiring lock on " + obj1);         synchronized (obj1) {             Organisation.out.println(proper noun + " caused lock on " + obj1);             piece of work();         }         Organisation.out.println(proper noun + " released lock on " + obj1);         Organization.out.println(name + " acquiring lock on " + obj2);         synchronized (obj2) {             System.out.println(name + " acquired lock on " + obj2);             piece of work();         }         System.out.println(name + " released lock on " + obj2);          System.out.println(name + " finished execution.");     }                                      
  • Lock Only What is Required: You should larn lock merely on the resource you have to work on, for example in above program I am locking the complete Object resource but if we are only interested in one of it's fields, and so we should lock but that specific field not consummate object.
  • Avoid waiting indefinitely: You can get deadlock if two threads are waiting for each other to stop indefinitely using thread join. If your thread has to wait for another thread to finish, it's always best to use join with maximum time you desire to wait for thread to end.

That'south all for deadlock in coffee threads.

steinbergwasellift41.blogspot.com

Source: https://www.journaldev.com/1058/deadlock-in-java-example

0 Response to "How Can We Create a Deadlock on a Servlet"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel