| By Sanat Vij | Article Rating: |
|
| September 7, 2011 07:00 AM EDT | Reads: |
8,847 |
A deadlock is a situation where two or more threads are blocked while waiting to obtain locks that some of the other threads in the deadlock are holding. Deadlocks can occur when multiple threads need the same locks, at the same time, but obtain them in different order. For instance, if thread 1 locks A, and tries to lock B, and thread 2 has already locked B, and tries to lock A, a deadlock arises. Thread 1 can never get B, and thread 2 can never get A. In addition, neither of them will ever know. They will remain blocked on each of their objects, A and B, forever.
Deadlock Detection
The most common way of detecting whether your application is deadlocked is by analyzing thread dumps of the Java Virtual Machine while its in the state of appearing hung. A thread dump is a snapshot of the virtual machine's current state, including stack traces for each Java thread.

The thread dump outputs the state of each thread, which may be one of the following:
- NEW: This state represents a new thread that is not yet started.
- RUNNABLE: This state represents a thread that is executing in the underlying JVM. Here executing in the JVM doesn't mean that the thread is always executing in the OS as well - it may wait for a resource from the operating system like the processor while being in this state.
- BLOCKED: This state represents a thread that has been blocked and is waiting for a moniotor to enter/re-enter a synchronized block/method. A thread gets into this state after calling the Object.wait method.
- WAITING: This state represnts a thread in the waiting state. This wait is over only when some other thread performs some appropriate action. A thread can get into this state either by calling - Object.wait (without timeout), Thread.join (without timeout), or LockSupport.park methods.
- TIMED_WAITING: This state represents a thread that is required to wait at max for a specified time limit. A thread can get into this state by calling any of these methods: Thread.sleep, Object.wait (with timeout specified), Thread.join (with timeout specified), LockSupport.parkNanos, and LockSupport.parkUntil
- TERMINATED: This state reprents a thread that has completed its execution either by returning from the run() method after completing the execution OR by throwing an exception that propagated from the run() method and hence caused the termination of the thread.
A thread that appears as "java.lang.Thread.State: BLOCKED (on object monitor)" in the stacktrace indicates that it's waiting to enter a synchronized block and acquire a Java monitor. Alternatively, the code may call an Object.wait method every time the lock is not acquired, causing the threads to appear as "java.lang.Thread.State: WAITING" in the stacktrace.
A deadlock is detected if a complete circle of such thrads can be seen in the thread dump where two or more threads are blocked/waiting, with each blocked/waiting thread waiting to acquire a lock on an object that has been locked by another blocked/waiting thread.
Example :
"Thread-1" prio=6 tid=0x02a99c00 nid=0xee0 waiting for monitor entr...
java.lang.Thread.State: BLOCKED (on object monitor)
at SimpleDeadLock$Thread2.run(SimpleDeadLock.java:37)
- waiting to lock <0x229bd238> (a java.lang.Object)
- locked <0x229bd240> (a java.lang.Object)
"Thread-0" prio=6 tid=0x02a99000 nid=0xefc waiting for monitor entr...
java.lang.Thread.State: BLOCKED (on object monitor)
at SimpleDeadLock$Thread1.run(SimpleDeadLock.java:24)
- waiting to lock <0x229bd240> (a java.lang.Object)
- locked <0x229bd238> (a java.lang.Object)
In the above thread dump, it can be seen that "Thread-0" and "Thread-1" are in a "BLOCKED" state. "Thread-0" holds a lock on object "0x229bd238" and is waiting to acquire a lock on object "0x229bd240", while at the same time "Thread-1" holds a lock on object "0x229bd240" and is waiting to acquire a lock on object "0x229bd238". Since each of the threads has acquired a lock on an object that the other thread needs in order to complete its operation, they will wait in the same state infinitely, causing the application to hang.
Acquiring Thread Dumps
A thread dump of a running JVM may be taken in various ways, a few of the methods are listed below:
- by sending a SIGQUIT signal to the JVM
kill -3 pid > threadDumpFilename 2>&1
- by using WebLogic scripting tool
export JAVA_HOME=<java home>
export WL_HOME=<WebLogic home>
cd WL_HOME/server/bin
. ./setWLSEnv.sh
java WebLogic.WLST
connect("username","password","t3://<url>:<port>")
cd ('Servers')
cd ('<server name>')
threadDump()
- by using WebLogic.Admin utility (deprecated)
export JAVA_HOME=<java home>
export WL_HOME=<WebLogic home>
cd WL_HOME/server/bin
. ./setWLSEnv.sh
java WebLogic.Admin <url>:<port> -username <username> -password <password> THREAD_DUMP
- by using WebLogic admin console
Login to AdminConsole->Server -> Monitoring -> Threads
- by using Jrockit command line utility (Jrockit JVM)
export JAVA_HOME=<java home>
export WL_HOME=<WebLogic home>
cd WL_HOME/server/bin
. ./setWLSEnv.sh
jrcmd <pid> print_threads
· by using the Jstack utility (HotSpot JVM)
export JAVA_HOME=<java home>
export WL_HOME=<WebLogic home>
cd WL_HOME/server/bin
. ./setWLSEnv.sh
jstack <pid>
Published September 7, 2011 Reads 8,847
Copyright © 2011 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Sanat Vij
Sanat Vij is a professional software engineer currently working at CenturyLink. He has vast experience in developing high availability applications, configuring application servers, JVM profiling and memory management. He specializes in performance tuning of applications, reducing response times, and increasing stability.
- Cloud People: A Who's Who of Cloud Computing
- BEA Updates WebLogic SOA Portal for Web 2.0 Era
- Cloud Expo NY: Best Practices for Delivering Oracle Database as a Service
- Velocity Technology Solutions Introduces IBM Power Systems Universal Cloud Services at COMMON 2013
- Cloud Business Solutions, Social Media, and Platform Systems of Engagement Market Shares, Strategies, and Forecasts, Worldwide, 2013 to 2019
- Lessons to Learn from the Hibernate Core Implementation
- Component Models in Java | Part 2
- Research and Markets: Global Platform-As-A-Service Market Expected To Post Revenue of US$6.45 Billion in 2016 According To Latest Report
- Java Method Size
- Part 3 | Component Models in Java
- BrightScope Releases Top 25 Technology Companies With the Best 401k Plans
- Tech Trends To Watch In May 2013
- Cloud People: A Who's Who of Cloud Computing
- BEA Updates WebLogic SOA Portal for Web 2.0 Era
- Global Micro Servers Market (2013 - 2018), By Processor Type (Intel, Arm, Amd), Component (Hardware, Software, Operating System), Application (Media Storage, Data Centers, Analytics, Cloud Computing) & Geography (North America, Europe, Apac, Row)
- Cloud Expo NY: Best Practices for Delivering Oracle Database as a Service
- Velocity Technology Solutions Introduces IBM Power Systems Universal Cloud Services at COMMON 2013
- Red Hat Spin-Off Simplifies Orchestration
- Componentizing Applications with Layered Architecture
- Cavalry Rides into Oracle’s Java Suit
- Cloud Business Solutions, Social Media, and Platform Systems of Engagement Market Shares, Strategies, and Forecasts, Worldwide, 2013 to 2019
- Component Models in Java | Part 1
- Part II: XtremIO, XtremSW and XtremSF EMC flash ssd portfolio redefined
- Global Information Security Products And Services Industry
- Java vs C++ "Shootout" Revisited
- Where Are RIA Technologies Headed in 2008?
- Configuring Eclipse for Remote Debugging a WebLogic Java Application
- XA Transactions
- Migrating a JBoss EJB Application to WebLogic
- An Introduction to Abbot
- Cloud People: A Who's Who of Cloud Computing
- 'HTTP Session Replication Failure' Issues
- WebLogic Tutorial: "Integrating Apache Poi in WebLogic Server"
- Eclipse "Pollinate" Project to Integrate with Apache Beehive
- Monitoring and Controlling WebLogic Servers with WLST
- Failover and Recovery of Enterprise Applications - Part 1





















