Showing posts with label Concurrency. Show all posts
Showing posts with label Concurrency. Show all posts

Friday, October 22, 2010

How to generate a threaddump for java based process

Windows
Can be captured in following ways

1) Using Ctrl-Break :
The Java application that you want to produce a thread dump for must be running / started in a command console. When you want to produce a thread dump press Ctrl-Break.

2) Using jstack :
step 1) Use jps command to find out the process id (pid) of the java process for which the thread dump is to be generated.
eg output for jps
C:\>jps
3316 Deadlock
3476 Jps
step 2) Use jstack command to along with the process id to generate the threaddump.
syntax : jstack
eg: c:/>jstack 3316

Linux or Solaris

1)Using Ctrl-\:
If the JVM is running in a console then simply press Ctrl-\.

2)Sending QUIT signal:
If the JVM is running in the background then send it the QUIT signal:

kill -QUIT process_id

There process_id is the process number of the running Java process. The thread dump will be sent to wherever standard out is redirected too.

You can generally get the process numbers of of all running Java processes with the command:

ps axf | grep java

3) Using jstack:
Steps as mentioned above in the Windows Section

Why Thread Dump is required ?

You need to generate threaddump of java based process whenever...
1) JVM refuses to exit cleanly.This could be due to
           a) Some user defined thread running infintely
           b) Some threads getting deadlock
           c) Some threads going in infinite wait state.
2) Application is unresponsive.
3) Application is not behaving as expected.

Friday, January 22, 2010

How to deal with deadlock ?

There are three possible approaches to dealing with deadlock:

  • Deadlock prevention — Deadlock can be prevented by ensuring that at least one of the four conditions required for deadlock never occurs. For example, by using sharable resources, never holding one resource while waiting for another, making resources preemptible, or by imposing a strict logical ordering on resource allocation requests so that all threads request resources in the same order.

  • Deadlock avoidance — If more information on the pattern of resource usage is known, then it is possible to construct an algorithm that will allow all the four conditions necessary for deadlock to occur, but which will also ensure that the system never enters a deadlock state. A deadlock avoidance algorithm will examine dynamically the resource allocation state and take action to ensure that the system can never enter into deadlock. Any resource allocation request that is potentially unsafe is denied.

  • Deadlock detection and recovery — In many general-purpose concurrent systems, the resource allocation usage is a priori unknown. Even if it is known, the cost of deadlock avoidance is often prohibitive. Consequently, many of these systems will ignore the problems of deadlock until they enter a deadlock state. They then take some corrective action (for example, by aborting a concurrent activity and preempting its resources.)

What are the conditions which can cause deadlock

There are four necessary conditions that must exist if deadlock is to occur.The conditions are as follows:

  • Mutual exclusion — only one concurrent activity can use a resource at once (that is, the resource is nonsharable or at least limited in its concurrent access); using locks as an example, mutual exclusion locks as opposed to read/write locks are more likely to cause deadlock.

  • Hold and wait — there must exist concurrent activities that are holding resources while waiting for others resources to be acquired; with locks, concurrent activities must be holding locks while waiting to acquire new locks.

  • No preemption — a resource can only be released voluntarily by a concurrent activity; the locks acquired by a concurrent activity cannot be forcibly taken away from it by another activity.

  • Circular wait — a circular chain of concurrent activities must exist such that each activity holds resources (has locked the objects) that are being requested by the next activity in the chain

Wednesday, January 20, 2010

Common Concurrency Challenges

1) Race Condition
Multiple threads access the same shared data without the appropriate locks on shared data.When this condition occurs, one thread may inadvertently overwrite data used by another thread, leading to both loss of information and data corruption.Minimun requirement for Race Condition to occur is 2 threads and 1 shared data object amongst them.

2) Thread Block
A thread calls a long-running operation while holding a lock thereby preventing the progress of other threads.When this condition occurs, application performance can drop dramatically due to a single bottleneck for all threads.

3) Deadlock
Two or more threads wait for locks in a circular chain such that the locks can never be acquired. When this condition occurs, the entire software system may halt, as none of the threads can either proceed along their current execution paths or exit. Minimum requirement for deadlock to occur is 2 threads and 2 shared data objects amongst these 2 threads.

What is Parallel Computing ?

Parallel Computing is the management of a collection of serial tasks

where:
management refers to the policies by which
(a) tasks are scheduled,
(b) premature terminations are handled,
(c) preemptive support is provided,
(d) communication primitives are enabled/disabled, and
(e) the manner in which resources are obtained and released,

and

serial tasks are classified in terms of either
(a) Coarse grain -- where tasks may not communicate prior to conclusion, or
(b) Fine grain -- where tasks may communicate prior to conclusion.