views:

256

answers:

4

Hi Experts,

Can somebody tell me how can I find out "how many threads are in deadlock condition" in Java multi-threading application? What is the way to find out the list of deaklock threads?

I heard about Thread Dump and Stack Traces but I don't know how to implement it.

  • I also want to know what all new features have been introduced in Java 5 for Threading?

Please let me know with your comments and suggestions.

+2  A: 

use kill -3 on the process id

this will print out to the console a thread dump and an overview of thread contention

simon622
Thank you! much appreciate.
Garhwali Bhai
+6  A: 

Way of obtaining thread dumps:

  • ctrl-break (Windows) or ctrl-\, possibly ctrl-4 and kill -3 on Linux/UNIX
  • jstack and your process id (use jps)
  • jconsole or visualvm
  • just about any debugger

Major new threading features in J2SE 5.0 (released 2004, in End Of Service Life Period):

  • java.util.concurrent
  • New Java Memory Model.
Tom Hawtin - tackline
Thank you very much Tom.
Garhwali Bhai
+1  A: 

From within your program, the ThreadMXBean class has a method findMonitorDeadlockedThreads(), as well as methods for querying the current stack traces of threads. From the console in Windows, doing Ctrl+Break gives you a list of stack traces and indicates deadlocked threads.

As well as some tweaks to the Java memory model that tidy up some concurrency "loopholes", the most significant feature underlyingly in Java 5 is that it exposes Compare-And-Set (CAS) operations to the programmer. Then, on the back of this, a whole raft of concurrency utilities are provided in the platform. There's really a whole host of stuff, but they include:

  • concurrent collections
  • executors, which effectively allow you to implement things such as thread pools
  • other common concurrency constructs (queues, latches, barriers)
  • atomic variables

You may be interested in some tutorials I've written on many of the Java 5 concurrency features.

Neil Coffey
A: 

If you want to learn about the new concurrent features in Java 5 you could do a lot worse than getting a copy of Java Concurrency in Practice by Brian Goetz (Brian Goetz and a number of the coauthors designed the Java 5 concurrency libraries). It is both highly readable and authoritative , and combining practical examples and theory.

The executive summary of the new concurrent utilities is as follows:

  • Task Scheduling Framework - The Executor framework is a framework for standardizing invocation, scheduling, execution, and control of asynchronous tasks according to a set of execution policies. Implementations are provided that allow tasks to be executed within the submitting thread, in a single background thread (as with events in Swing), in a newly created thread, or in a thread pool, and developers can create of Executor supporting arbitrary execution policies. The built-in implementations offer configurable policies such as queue length limits and saturation policy which can improve the stability of applications by preventing runaway resource consumption.
  • Concurrent Collections - Several new Collections classes have been added, including the new Queue and BlockingQueue interfaces, and high-performance, concurrent implementations of Map, List, and Queue.
  • Atomic Variables - Classes for atomically manipulating single variables (primitive types or references), providing high-performance atomic arithmetic and compare-and-set methods. The atomic variable implementations in java.util.concurrent.atomic offer higher performance than would be available by using synchronization (on most platforms), making them useful for implementing high-performance concurrent algorithms as well as conveniently implementing counters and sequence number generators.
  • Synchronizers - General purpose synchronization classes, including semaphores, mutexes, barriers, latches, and exchangers, which facilitate coordination between threads.
  • Locks - While locking is built into the Java language via the synchronized keyword, there are a number of inconvenient limitations to built-in monitor locks. The java.util.concurrent.locks package provides a high-performance lock implementation with the same memory semantics as synchronization, but which also supports specifying a timeout when attempting to acquire a lock, multiple condition variables per lock, non-lexically scoped locks, and support for interrupting threads which are waiting to acquire a lock.
  • Nanosecond-granularity timing - The System.nanoTime method enables access to a nanosecond-granularity time source for making relative time measurements, and methods which accept timeouts (such as the BlockingQueue.offer, BlockingQueue.poll, Lock.tryLock, Condition.await, and Thread.sleep) can take timeout values in nanoseconds. The actual precision of System.nanoTime is platform-dependent.
fmark