Java Concurrency Deadlock

java-concurrency

http://www.javaworld.com/article/2074310/java-app-dev/java-hanging-thread-detection-and-handling.html
http://coopsoft.com/ar/stalledarticle.html
http://javaeesupportpatterns.blogspot.com/2013/09/oracle-weblogic-stuck-thread-detection.html
https://blogs.oracle.com/oem/quickly-diagnose-the-root-cause-of-stuck-threads-using-oracle-enterprise-manager-12c-jvm-diagnostics-v3
https://dzone.com/articles/oracle-weblogic-stuck-thread
https://stackoverflow.com/questions/11407405/how-can-i-debug-a-hanging-java-thread
https://stackoverflow.com/questions/830356/automatically-detect-hanging-threads
https://stackoverflow.com/questions/20891386/how-to-detect-thread-being-blocked-by-io
http://www.munzandmore.com/2012/ora/weblogic-stuck-threads-howto
https://coderanch.com/t/440481/java/handle-kill-hung-thread

http://yusuke.homeip.net/samurai/en/index.html
http://java.net/projects/tda
http://mchr3k.github.com/javathreaddumpanalyser/
https://spotify.github.io/threaddump-analyzer/
http://www.javaworld.com/article/2072865/thread-analysis-with-visualvm.html
http://samuraism.jp/samurai/en/index.html
https://planet.jboss.org/post/simple_tools_to_analyze_thread_dumps
http://fastthread.io/
https://www.site24x7.com/tools/thread-dump-analyzer.html
https://www.javacodegeeks.com/2012/03/jvm-how-to-analyze-thread-dump.html
http://korhner.github.io/java/multithreading/detect-java-deadlocks-programmatically/
http://sergebeauchamp.blogspot.com/2010/11/how-to-debug-and-detect-deadlocks.html
https://kellicker.wordpress.com/2010/05/03/deadlock-debugging-in-eclipse/

// Dead lock happens when threadA is waiting for threadB, but threadB is 
// also waiting for threadA.

public class TestThread {
   public static Object Lock1 = new Object();
   public static Object Lock2 = new Object();

   public static void main(String args[]) {
      ThreadDemo1 T1 = new ThreadDemo1();
      ThreadDemo2 T2 = new ThreadDemo2();
      T1.start();
      T2.start();
   }

   private static class ThreadDemo1 extends Thread {
      public void run() {
         synchronized (Lock1) {
            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 (Lock2) {
               System.out.println("Thread 1: Holding lock 1 & 2...");
            }
         }
      }
   }
   private static class ThreadDemo2 extends Thread {
      public void run() {
         synchronized (Lock2) {
            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 (Lock1) {
               System.out.println("Thread 2: Holding lock 1 & 2...");
            }
         }
      }
   } 
}

To fix the example above, we just have to reverse the order of
the locks.  Other deadlock scenarios are more complicated, and we may
need to use a semaphore or get a lock on another object.

To detect deadlock we can use static code analysis tools such as FindBugs,
or CheckThread.  We can also detect dead locks using the ThreadMXBean
interface.

We can also use jProfiler, or JStack

In order to analyze a thread dump, you need to know the status of threads. The 
statuses of threads are stated on java.lang.Thread.State.

NEW: The thread is created but has not been processed yet.
RUNNABLE: The thread is occupying the CPU and processing a task. 
  (It may be in WAITING status due to the OS's resource distribution.)
BLOCKED: The thread is waiting for a different thread to release its lock 
  in order to get the monitor lock.
WAITING: The thread is waiting by using a wait, join or park method.
TIMED_WAITING: The thread is waiting by using a sleep, wait, join or 
  park method. (The difference from WAITING is that the maximum waiting 
  time is specified by the method parameter, and WAITING can be relieved by
  time as well as external changes.) 

Java threads can be divided into two:
1. daemon threads
2. non-daemon threads

Daemon threads stop working when there are no other non-daemon threads. Even 
if you do not create any threads, the Java application will create several 
threads by default. Most of them are daemon threads, mainly for processing 
tasks such as garbage collection or JMX.

A thread running the 'static void main(String[] args)’ method is created as a 
non-daemon thread, and when this thread stops working, all other daemon 
threads will stop as well. (The thread running this main method is called 
the VM thread in HotSpot VM.)

jps -v
jstack -f 5824

jVisualVM

When using Java.lang.Thread class to generate a thread, the thread will be 
named Thread-(Number), whereas when using java.util.concurrent.ThreadFactory 
class, it will be named pool-(number)-thread-(number).
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License