{"id":244,"date":"2009-06-20T14:58:11","date_gmt":"2009-06-20T19:58:11","guid":{"rendered":"http:\/\/appcrawler.com\/wordpress\/?p=244"},"modified":"2011-07-06T09:52:54","modified_gmt":"2011-07-06T14:52:54","slug":"whos-blocking-my-java","status":"publish","type":"post","link":"http:\/\/appcrawler.com\/wordpress\/2009\/06\/20\/whos-blocking-my-java\/","title":{"rendered":"Who\u2019s blocking my java?"},"content":{"rendered":"<p>Hanging java applications can be a system architects worst nightmare.  There are so many pieces in the stack, finding where the true issue lies can be a daunting task.  Fortunately, you can dump all threads in a JVM to see where it may be hanging.  <\/p>\n<p>DBA&#8217;s specifically should be able to read a thread dump, as the finger will often be pointed at the database as the source of the problem, when that may not be the case.  If you can&#8217;t find any issues in the database that would indicate it could be the source of a hang, you should immediately ask for a thread dump in the application.  A lot of times ( I have personally witnessed it ), the application support team will look at you with a blank stare and you won&#8217;t hear anymore about it (note this isn&#8217;t good for your customer if the problem isn&#8217;t fixed \ud83d\ude41 )<\/p>\n<p>To give you an idea as to what this may look like, below is an example of a class that fabricates a blocking thread within a multi-threaded java application.  We then show how to prove this using a thread dump.<\/p>\n<pre lang=\"java\" line=\"1\">\r\nclass syncCounter {\r\n  private int c = 0;\r\n\r\n  public synchronized void increment() {\r\n    try {\r\n      Thread.sleep(10000);\r\n      c++;\r\n    }\r\n    catch(Exception e) {}\r\n  }\r\n}\r\n\r\nclass syncRun implements Runnable {\r\n  Thread t;\r\n  static syncCounter s;\r\n\r\n  public static void main(String args[]) throws Exception {\r\n    s = new syncCounter();\r\n    for (int i = 1; i <= 10; i++) {\r\n      syncRun tt = new syncRun(\"Thread \" + i);\r\n    }\r\n  }\r\n\r\n  syncRun(String tname) {\r\n    t = new Thread(this,tname);\r\n    t.start();\r\n  }\r\n\r\n  public void run() {\r\n    s.increment();\r\n    System.out.println(\"Running thread \" + t.getName());\r\n  }\r\n}\r\n<\/pre>\n<p>When we run this application at the command line, the first thread locks the object monitor (the synchronized block of code above) for ten seconds before releasing it.  The other nine threads will be blocked by this thread.  The next thread to get the lock will block the next eight, and so on.  The tenth thread will be blocked from running for at least 100 seconds as a result.<\/p>\n<p>We compile the class and run it.  After this, we find the java process by running the jps executable, and then run jstack against it and redirect the output to a file.  Note you could also issue a kill -QUIT on *nix platforms to generate the stack trace.<\/p>\n<pre lang=\"text\">\r\nlinux5:root:orcl11g:\/root>jps\r\n28349 syncRun\r\n28366 Jps\r\nlinux5:root:orcl11g:\/root>jstack 28349 > stack.txt\r\n<\/pre>\n<p>When we review the stack.txt file, we see one thread something similar to the following...<\/p>\n<pre lang=\"text\">\r\n\"Thread 8\" prio=6 tid=0x02aac000 nid=0x166c waiting on condition [0x0304f000..0x0304fb94]\r\n   java.lang.Thread.State: TIMED_WAITING (sleeping)\r\n\tat java.lang.Thread.sleep(Native Method)\r\n\tat syncCounter.increment(syncCounter.java:6)\r\n\t- locked <0x229bd478> (a syncCounter)\r\n\tat syncRun.run(syncCounter.java:42)\r\n\tat java.lang.Thread.run(Thread.java:619)\r\n<\/pre>\n<p>...and the nine other threads created by our sample program, each of which look  like...<\/p>\n<pre lang=\"text\">\r\n\"Thread 7\" prio=6 tid=0x02aaac00 nid=0x1330 waiting for monitor entry [0x02fff000..0x02fffd14]\r\n   java.lang.Thread.State: BLOCKED (on object monitor)\r\n\tat syncCounter.increment(syncCounter.java:6)\r\n\t- waiting to lock <0x229bd478> (a syncCounter)\r\n\tat syncRun.run(syncCounter.java:42)\r\n\tat java.lang.Thread.run(Thread.java:619)\r\n<\/pre>\n<p>If you notice, all nine threads blocked will show a \"waiting to lock\" for the same address as the thread that has the lock.  If you can prove this, you just need to review the java code (and specific lines) noted in the thread dump and ascertain why it is blocking.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hanging java applications can be a system architects worst nightmare. There are so many pieces in the stack, finding where the true issue lies can be a daunting task. Fortunately, you can dump all threads in a JVM to see&hellip;<\/p>\n<p class=\"more-link-p\"><a class=\"more-link\" href=\"http:\/\/appcrawler.com\/wordpress\/2009\/06\/20\/whos-blocking-my-java\/\">Read more &rarr;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"footnotes":""},"categories":[24,25],"tags":[],"_links":{"self":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts\/244"}],"collection":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/comments?post=244"}],"version-history":[{"count":15,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts\/244\/revisions"}],"predecessor-version":[{"id":250,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts\/244\/revisions\/250"}],"wp:attachment":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/media?parent=244"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/categories?post=244"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/tags?post=244"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}