Wednesday, May 15, 2013

Speaker @ JavaOne Hyderabad India 2013 Conference

Recently I had given a talk @ JavaOne Hyd India 2013 on topic "Sequential Concurrency .... WHAT ???".
Below is the presentation.
Snaps of JavaOne can be found at also on our DaNJUG FaceBook page

Sunday, November 25, 2012

Sequential Thread Pool

In continuation to the earlier post. Now I am publishing a draft version the Sequential Thread Pool document. You can get a copy of document from HERE

Friday, November 11, 2011

Multi-Queue Or FSM ThreadPool, Could This Be A New Design Pattern

ThreadPool Introduction
      As we all know that normal ThreadPool contains the number of threads and all those threads takes the tasks from the single common queue present in the ThreadPool. But in this type of thread pool we cannot ensure the order of processing of the tasks present in the queue. These thread pool are best where we do not have any dependency on the other task(s) present in the queue.
      But there are certain applications that requires that the order of the processing of the Tasks should be maintained in which they came/arrive in the application. When Application wants to maintain the order of the events/tasks when processing them. If we use the normal ThreadPool in this case, this will not work as it does not ensure the task processing order.

What is Multi-Queue Or FSM(Finite State Machine) ThreadPool
      As we discussed above, where normal ThreadPool fails. So I though of a multi-queue ThreadPool which maintains the sequence of the tasks in which they came and process those tasks in the specified order only.
      If we see any FSM (Finite State Machine), we generally describe the flow of the machine using FSM. In FSM, flow normally goes from one state to another state and in Mealy Machines result depends on the input provided and current state of machine.
      If we have some objects in our application that have some states associated with that object and that can be changed based on the input and its current state, in this condition we must use the FSM-ThreadPool to solve our problem.
      FSM ThreadPool can only be used in your application, if your objects are changing their states based on some inputs/events/tasks which is being/to-be processed by the ThreadPool.
Key Elements of FSM ThreadPool
  • KeyObject (KO): Object which is keep on changing its state and it's state change should be processed sequentially.
  • StateChangeTask (SCT/Task): Task to be performed to change the state of the KO.
Every SCT/Task must have the information to identify its KO.

When you need the FSM ThreadPool
  • When you are having some decoupled systems
  • These decoupled systems communicates in asynchronous manner via events/messages.
  • When you actually wanted to process the events/messages in the order they arrive in the systems from other system.
  • When each state change on a specific object(KO) is treated independently.

How FSM-ThreadPool works
      Well, in simple ThreadPool we have many threads running or in IDLE state, those takes tasks from a single common queue. But in FSM ThreadPool we have many threads and multi-queue concept. We manage the multiple queues and multiple Threads those will take the tasks from the queue and process them.

FSM ThreadPool

      Why we need the multi-queue here? As we have discussed above that there can be many KOs in your application and their state change must be processed Sequentially. So if one KO is changing its state from x-> y and second KO is also changing its state from let say p->q then processing of the tasks of both the KO can be done simultaneously, as those objects are not dependent on each other. To maintain the sequence of the SCTs/Tasks processed on a KO we should handle them sequentially in a single queue. Our ThreadPool should be able to identify the unique KO associated with the SCT/Task and keep those SCT/Task into the same queue, so that those can be processed sequentially.

Multi-threading along with Sequential execution
Lets take an example of an application which is having multiple KOs

M0 => M0-T1, M0-T2, M0-T3 . . . . . . . . M0-Tn
M1 => M1-T1, M1-T2, M1-T3 . . . . . . . . M1-Tn

M0 and M1 are both KOs objects and their associated Tasks/SCTs in sequence are in the form M<ko-id>-T<taskseq>

As we can see that M0 and M1 both are independent of each other, they do not have any dependency. Those KO's behaviors are only dependent on their Tasks those are coming in sequential order. So it is possible that two separate threads T1 and T2 can process the tasks of M0 and M1 simultaneously.

Keep watching for this blog, to see the various implementations...

Sunday, July 24, 2011

Is It Possible To Catch OutOfMemoryError

Many of the Java techies wanted to know "Is it possible to catch the OutOfMemoryError". YES, we can catch this Error, because it is extending from Throwable class. You can release some memory when this exception occurs.
If there are some non daemon threads are running those do not do any memory related operation then the application will continue running, it will not corrupt otherwise it will.
See the below example in which we are releasing memory :

package exception;

import java.util.ArrayList;
import java.util.List;

public class OutOfMemoryErrorTest {

 static List lst = new ArrayList();
 public static void main(String args[])
   Thread thr = new Thread( new Runnable() {
    public void run() {
      try {
      } catch (InterruptedException e) {
   while(true) {
  }catch(OutOfMemoryError e ) {
   System.out.println("Error Catched.....");
   System.out.println("Memory released...");

Output :

java.lang.OutOfMemoryError: Java heap space
Error Catched.....
 at java.util.Arrays.copyOf(
 at java.util.Arrays.copyOf(
 at java.util.ArrayList.ensureCapacity(
 at java.util.ArrayList.add(
 at exception.OutOfMemoryErrorTest.main(
Memory released...

As we can see in the output, we can catch the OutOfMemoryError and we released the memory and application continue to work as normal without being corrupted.


  • If you are getting OutOfMemoryError in your application then it might be possible that you are not handling the objects properly means objects are not getting garbage collected, because you are keep on storing those objects in memory.
  • When java application sees that the memory is being short then JVM first triggers the GC and see if that releases the memory. If GC is unable to clean the objects as those objects are strongly referenced then this leads to the OutOfMemoryError.
  • If your application needs more memory to run stably then you need to give your application more memory by specifying –Xmx in VM arguments.

Friday, July 9, 2010

Detect Deadlock And Dump Threads of a Running Java Process

jstack Utility

This is a utility provided by the JDK to do the following operations
  • Dump the threads of a running java application
  • To find the deadlock in the java application
To use this utility you need to know the process ID of the Java application. To know the process ID of all running Java processes simply use the "jps" command in your command prompt.This shows the PID and the process name.

Choose the appropriate PID for which you want the Thread Dump and run the "jstack" command. like

jstack <PID>

This will output the Thread Dump on console, you can also redirect the output to the file.

To show the Deadlock I run a program which is actually created DeadLock in the application. You can see the application is running in jps command output. Name of the application is "AnotherDeadLock". I took the ThreadDump of this application into a file as specified above and used Samurai tool to analyize the Thread Dump log file. Below is the output.