Thursday, June 23, 2016

Race Condition in Java

Many of us might be aware of Synchronization in Multi-threading environments in java. 
Race condition in Java is a type of concurrency error which is introduced in your program because  parallel execution of your program by multiple threads at same time, Since Java is a multi-threaded programming language hence risk of Race condition is higher in Java which demands clear understanding of what causes a race condition and how to avoid that. 

To Find Race conditions is very difficult, though since readability of Java code is very good and synchronized constructs are well defined heaps to find race conditions by code review. One can not find the race condition at the time of unit testing. Because race condition is the situation that won't occur often.  You can find the race condition in your program by reviewing it manually or you can use best code review tools available in the marketplace(try to find one such tool and let me know if you found that :).


Mostly it is reviewed that the collections like HashMap must be used with very much care as this is non-synchronized and multiple threads can apply for the re-size operation at the same time. Hence, it is recommended for you that if you are working in the multi-threaded application then use ConcurrentHashMap or use the synchronized HashMap which is again not recommended because of low performance in the multi-threaded environment.



Thursday, June 16, 2016

Executor Framework in Java with Examples

Being a java developer, I strongly recommend to have a thorough knowledge of this topic. Let's start the journey.

Let's go through some basic terms before jumping into the pool so that the things will be clear when we will use them.

* Process -> A program in execution which takes its separate memory space.
* Task -> A task is a logical unit of work.
* Thread -> The mechanism by which the Tasks can be executed asynchroneously. Asynchroneously means on the independent paths by not interfering into other tasks.

We have two policies for executing tasks using threads as follows:-

1) Execution of tasks sequentially using single thread.
2) Execution of each task in it's own thread.

In the first approach, one thread is created at all and the tasks are assigned to it in the sequential manner so that one task can be picked up after completion of another.

In the second approach, a separate thread is created for each task so that each task can be performed independently without waiting.

Executor framework takes birth here because both of the upper approaches have limitations as follows:

Problem with First Approach : It suffers from poor responsiveness and poor throughput.
Problem with Second Approach : It suffers with poor resource management as thread needs to be created for each task (for example,one million threads needed if there are million tasks).

Now the curiosity is how Executor Framework helps out in these scenarios.....

Executor Framework says that:
(i) Create a fixed number of threads in the application, suppose 10. These will be called as Worker Threads.
(ii) Create the tasks and submit it to executor to execute.

Suppose, we have created 10 fixed threads and we have 100 tasks to execute. Then, these ten threads will serve ten tasks at a time and when any task will be completed, the thread which executed this task is free for another task to be assigned to it. Hence the basic idea is , these 10 worker threads will execute these 100 tasks non stop. As and when any thread will be free, it will pick up the next task from the queue.
 
Now, let's discuss about tasks, where are these stored and how they are passed to executor. BlockingQueue is used for the purpose to hold the tasks. When any thread is free, it needs to dequeue the task from the BlockingQueue and execute it.

Executor : Executor is an interface having "void execute(Runnable command)" method.
Executors : Executors is a class having multiple methods to create the pool of worker threads.
ExecutorService : ExecutorService is an interface which extends Executor interface with some extra methods of submitting the tasks,
ScheduledExecutorService : ScheduledExecutorService is another interface with extends ExecutorService interface and provides some extra methods of scheduling the tasks.

Now, let's have an example to explain this all.

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class ExecutorDemo {

    private static ExecutorService executor = null;
    private static volatile Future firstThread = null;
    private static volatile Future secondThread = null;

    public static void main(String[] args) {
        executor = Executors.newFixedThreadPool(2);
        while (true)
        {
            try
            {
                checkTasks();
                Thread.sleep(1000);
            } catch (Exception e) {
                System.err.println("Caught exception: " + e.getMessage());
            }
        }
    }

    private static void checkTasks() throws Exception {
        if (firstThread == null
                || firstThread.isDone()
                || firstThread.isCancelled())
        {
            firstThread = executor.submit(new ThreadOne());
        }

        if (secondThread == null
                || secondThread.isDone()
                || secondThread.isCancelled())
        {
            secondThread = executor.submit(new ThreadTwo());
        }
    }
}

class ThreadOne implements Runnable {
    public void run() {
        while (true)
        {
            System.out.println("Executing thread one");
            try
            {
                Thread.sleep(1000);
            } catch (Throwable e) {
                e.printStackTrace();
            }
        }

    }
}

class ThreadTwo implements Runnable {
    public void run() {
        while (true)
        {
            System.out.println("Executing thread two");
            try
            {
                Thread.sleep(1000);
            } catch (Throwable e) {
                e.printStackTrace();
            }
        }
    }
}

First of all we have created two tasks here as ThreadOne and ThreadTwo. We created the threadpool of worker threads of size 2 by the method newFixedThreadPool(2). then the checkTasks method get called to check if any thread has been done or cancelled. If any task has been done or cancelled then it is getting submitted again to the executor by executor.submit(new ThreadTwo());.

Hence, we have created two threads but are not starting them explicitly, we are just submitting them to executor and these will be executed as per the execution logic.

Friday, June 10, 2016

Java : Usage of ThreadPool in java with Example

Every concept is very big,troublesome and non-understandable if the developer thinks in mind that the concept is tough enough. Many of the java developers would be thinking same about ThreadPool and Executor Framework that it is a very tough topic in concurrency. Believe me , it is the easiest topic I have ever learned. Just follow me to prove that.

First of all let us see the Basics of a Thread : In any multi-threading application, if the developer is using the Thread is by following the below mentioned procedure:-

1) Create a Thread using Runnable or Thread class.
2) Call the start() method over the threads to execute the above created Thread.

Hence, the developer have to take care of creating thread and calling it explicitly. What if the developer doesn't need to create the Thread everytime he needs it? What if the developer doesn't need to call the thread everytime he needs it? That's what ThreadPool does.

ThreadPool is nothing but a good procedure to follow to separate this task of Thread Creation and Execution.

It means we don't need to worry about Thread creation . ThreadPool will handle this task . How ? will see in some moments.

Now, Basic Idea Behind Using ThreadPool:

Let's take an example before jumping in which will illustrate the difficulties if we don't use ThreadPool. Suppose we create Threads in our Multi-threaded application to handle the different tasks. Or , suppose we make the any type of connections per user when the user enters the application. Now, there might be two issues:

1) There might be the limitation to JVM as how number of threads can be created in the application.
2) There might occur OutOfMemory error as there might be limit of memory to hold the Threads and Execute them.

Hence, we need to limit the number of users can enter in the application at a time but it is a big bottleneck for the application to limit the number of users. Hence , we are in trouble . What to do. Thanks to java for ThreadPool implementation. Let's see how ThreadPool handles this:

ThreadPool creates a number of worker Threads at any point of time. You will decide at least how many threads you will need to handle the requests of your application. We have separate methods of the utility class "Executors" for that purpose. Executors is a class and Executor is an interface. For Ex:

ExecutorService service = Executors.newFixedThreadPool(10);    //ExecutorService is another interface which extends Executor interface

Below three can be used to create a ThreadPool:

ExecutorService executorService1 = Executors.newSingleThreadExecutor(); //To create single thread

ExecutorService executorService2 = Executors.newFixedThreadPool(10);

ExecutorService executorService3 = Executors.newScheduledThreadPool(10); // To create a number of threads with time scheduling

Now, the story come here like :- if you have passed 10 in the constructor then 10 worker threads would be created and the game starts.

We just need to submit our tasks to the Executor and it will pick any task from them and will execute it. Very clearly it means that these 10 threads would be called as worker threads. Suppose we have 100 tasks to be done and there are only 10 Threads created using ThreadPool. Now, all of these tasks would be busy executing those tasks one by one. Suppose one Thread has finished it's first task, then it will come in the idle state and will pick the another task from the Blocking Queue. What I mean by Blocking Queue is that to put the tasks in a collection , Blocking Queue is used. So the idle Thread would first Deblock the task from Blocking Queue and then would execute this. That's the magic of ThreadPool,using which we don't need to create hundred Thread at run time for processing hundred tasks. Rather 10 fix wroker threads done the work of hundred requests.

Hence, the work of Creation of Threads and work of Submission is separated by using ThreadPool.

Let's understand all this by the following Example:

public class ThreadPoolExample {

    public static void main(String args[]) {
       ExecutorService service = Executors.newFixedThreadPool(10);
       for (int i =0; i<100; i++){
           service.submit(new TaskToPerform(i));
       }
    }
  
}

In the above class, we have used the fixedThreadPool to create a ThreadPool of fixed size of 10. newFixedThreadPool is a static method of Executors class and the service reference is put in the ExecutorService interface. This class calls the submit method to submit 100 tasks (tasks here are the instances of TaskToPerform class). Hence, 10 threads are responsible to do 100 tasks. No new thread is getting created at run time and not .start() method is getting called from anywhere. All that is abstract to us. All that happends internally. Now, create the TaskToPerform thread.

final class TaskToPerform implements Runnable{
    private int taskId;
  
    public TaskToPerform(int id){
        this.taskId = id;
    }
  
    @Override
    public void run() {
        System.out.println("Task ID : " + this.taskId +" performed by " 
                           + Thread.currentThread().getName());
    }
  
}

Here, the Thread is created implementing Runnable interface. When we run this program produces the following output, Hence, it is proved that pool-1 is having multiple threads created and are being called one by one to perform the tasks.

Output:
Task ID : 0 performed by pool-1-thread-1
Task ID : 3 performed by pool-1-thread-4
Task ID : 2 performed by pool-1-thread-3
Task ID : 1 performed by pool-1-thread-2
Task ID : 5 performed by pool-1-thread-6
Task ID : 4 performed by pool-1-thread-5

Monday, June 6, 2016

Java : Usage of ThreadGroup with Example

All of you from java family must be aware about the packaging concept. Why we create a package in java? We create a package in java just for the categorization of the classes of similar types in separate groups. You might be thinking why I started the package concept under the ThreadGroup. Actually, if I talk in one line, ThreadGroup is nothing but the categorization of the similar threads in separate groups. I hope I am clear.

For better understanding let's take the following points as an example:

1) System Threads are the group of threads which system takes care of. like: Main thread is under the category of System Threads. So, main is a sub-group of System Threads and System Threads can be named as System Thread Group.

2) Suppose I created Thread1, Thread2 and Thread3 in the main method then These can be put in the Sub-Group1 which can be considered as a ThreadGroup. Although, there might be multiple groups of threads like Sub-Group1 hence all will be the ThreadGroups of different kinds. One group of Threads might be given the task of contacting to database. Another group of Threads might be given the task of calculating the business logic, one another group of Threads might be given the task of showing the result on UI. Hence, all of these will be under such Groups which will be called ThreadGroups. The main point to note here is that Main would be the parent ThreadGroup of these sub-groups.

Now, lets discuss about the constructors of ThreadGroup so that we can come to know how to create ThreadGroup in our java multi-threading application:

public static void main (String [] args){
   ThreadGroup group1 = new ThreadGroup ("Group1");         // constructor1
   ThreadGroup group2 = new ThreadGroup (group1, "Group2"); // constructor2
}

In the constructor1, a ThreadGroup is created by just giving the ThreadGroup name.
In the constructor2, a ThreadGroup is created by giving the ThreadGroup name plus the parent name of this ThreadGroup as the first argument.

In the constructor1, it will be considered that the parent of this ThreadGroup is main itself(if it is defined in main or any other context where it would be defined).


How to destory ThreadGroup created in the application:

Once you no longer need the ThreadGroup objects, you can call ThreadGroup's destroy() method like below:

group1.destroy();      // group1 is the reference of the ThreadGroup

Here is some twist in the story: If the top ThreadGroup reference calls destroy() then all the sub groups will be available for the garbage collection because if the parent is destroyed the child ThreadGroups will automatically be nullify and would be available for the garbage collection.Otherwise, destroy() throws an IllegalThreadStateException object.


Let us demonstrate the above story by the below example:

class ThreadGroupDemo
{
   public static void main (String [] args)
   {
      ThreadGroup group1 = new ThreadGroup ("sub-group1");
      Thread t1 = new Thread (group1, "thread 1");
      Thread t2 = new Thread (group1, "thread 2");
      Thread t3 = new Thread (group1, "thread 3");
      group1 = new ThreadGroup (group1,"sub-group2");
      Thread t4 = new Thread (group1, "my fourth thread");
      group1 = Thread.currentThread ().getThreadGroup ();
      int groupCount = group1.activeGroupCount ();
      System.out.println ("Active thread groups in " + group1.getName () + " thread group: " + groupCount);
      group1.list ();
   }
}

Output would be like:

Active thread groups in main thread group: 2
java.lang.ThreadGroup[name=main,maxpri=10]
    Thread[main,5,main]
    java.lang.ThreadGroup[name=subgroup 1,maxpri=10]
        java.lang.ThreadGroup[name=subgroup 2,maxpri=10]


In the example code above, sub-group1 is the sub ThreadGroups of main ThreadGroup. sub-group2 is the sub ThreadGroup of sub-group1.

Now let's talk something about the Priority of ThreadGroups:

Suppose a ThreadGroup is having five Threads and one of the thread can attain a maximum priority from these five threads, then that priority will be the ThreadGroup's maximum priority. Use ThreadGroup's void setMaxPriority(int priority) method to subsequently set the maximum priority.Any threads that you add to the group after setting its maximum priority cannot have a priority that exceeds the maximum. It means if the max priority of the ThreadGroup is set to 6 then any of the threads of this group can not have a priority 7. So, any thread with a higher priority automatically lowers when it joins the thread group. Although, vise-versa is not true i.e. However, if you use setMaxPriority(int priority) to lower a group's maximum priority, all threads added to the group prior to that method call keep their original priorities. For example, if you add a priority 8 thread to a maximum priority 9 group, and then lower that group's maximum priority to 7, the priority 8 thread remains at priority 8.

Let's have an example to demonstrate it:

class MaxPriorityDemo
{
   public static void main (String [] args)
   {
      ThreadGroup group1 = new ThreadGroup ("A Group");
      System.out.println ("group1 maximum priority = " + group1.getMaxPriority ());
      Thread thread1 = new Thread (group1, "X Thread");
      System.out.println ("thread1 priority = " + thread1.getPriority ());
      thread1.setPriority (Thread.NORM_PRIORITY + 1);
      System.out.println ("thread1 priority after setPriority() = " + thread1.getPriority ());
      group1.setMaxPriority (Thread.NORM_PRIORITY - 1);
      System.out.println ("group1 maximum priority after setMaxPriority() = " + group1.getMaxPriority ());
      System.out.println ("thread1 priority after setMaxPriority() = " +thread1.getPriority ());
      Thread thread2 = new Thread (group1, "Y Thread");
      System.out.println ("thread2 priority = " + thread2.getPriority ());
      thread2.setPriority (Thread.NORM_PRIORITY);
      System.out.println ("thread2 priority after setPriority() = " + thread2.getPriority ());
   }
}

Output would be like that:

group1 maximum priority = 10
thread1 priority = 5
thread1 priority after setPriority() = 6
group1 maximum priority after setMaxPriority() = 4
thread1 priority after setMaxPriority() = 6
thread2 priority = 4
thread2 priority after setPriority() = 4


Now, let's talk about the ThreadGroup Interuption:

In addition to using ThreadGroups to limit thread priority, you can accomplish other tasks by calling various ThreadGroup methods that apply to each group's thread. Methods include void suspend(), void resume(), void stop(), and void interrupt(). Because Sun Microsystems has deprecated the first three methods (they are unsafe), we examine only interrupt().

ThreadGroup's has an interrupt() method that allows a thread to interrupt a specific ThreadGroup's all threads and subgroups. Main thread creates multiple threads and each perform a unit of work. Because all threads must complete their work before any thread can examine the results, each thread waits after completing its work. The main thread monitors the working state. When all other threads are waiting, the main thread calls interrupt() to interrupt the other threads' waits. Then those threads can examine and process the results.




Sunday, June 5, 2016

Java : Usage of ThreadLocal with Example

ThreadLocal is a class in java.lang package. The instance of this class is used as a local variable of the Thread in which it is created. Means that ThreadLocal variable is the personal storage of any Thread. Means that ThreadLocal instance stores the values of the local variables of the Threads. Most important point to note here is that the value of the ThreadLocal variable is different for all the Threads. It means when the single Thread will be shared within multiple objects , each time the ThreadLocal variable's value will be differently stored for each Thread as per the user implementation.

Although, it seems that ThreadLocal is taking the place of Synchronization but is not purely true. Partially we can say that Synchronization and ThreadLocal seems to work similar because in Synchronization also the functionality is similar in the sense that one thread can access the safe area at a time and get or set the values freely without any fear. On the other hand ThreadLocal variable plays the same role for each thread having different values for each Thread . Hence, in some situations if we see in our application that the Synchronization costs too much , we can use ThreadLocal variable and perform our task smoothly because the value of the variables would be different for different thread as per hold in the ThreadLocal variable. Let's take an example to explain better this concept:

public class ThreadLocalExampleClass {

    public static class MyRunnable implements Runnable {

        private ThreadLocal<Integer> threadLocal = new ThreadLocal<Integer>();

        @Override
        public void run() {
            threadLocal.set( (int) (Math.random() * 100) );   
            System.out.println(threadLocal.get());
        }
    }

    public static void main(String[] args) throws InterruptedException {
        MyRunnable threadInstance = new MyRunnable();

        Thread thread1 = new Thread(threadInstance);
        Thread thread2 = new Thread(threadInstance);

        thread1.start();
        thread2.start();

    }
}

In the example above, we have created a Thread by implementing Runnable interface, and in the run method we are generating a random number and setting it to the theadLocal variable.

Then, we created the thread instance in the main method and started two threads on the same object. Now, when the first thread will enter the run method the random number generated will be set into this threads's threadLocal variable and when the second thread will enter the run method , again the instance of threadLocal will be created and the random value will be set into that. Hence, two threads on the same object will get and set the different values on the ThreadLocal variable. If we do the same without using ThreadLocal and Synchronization then we can have race condition problem when we are sharing some resource between multiple threads simultaneously. Other way out of this situation is Synchronization but it might be too costly sometimes because one thread will enter the shared object at a time.

Hence, we can conclude that using ThreadLocal variables in the Multi-Threading applications is very helpfull in maintaining the different values for differnt Threads without using Synchronization. This is very usefull in the product selling sites because there the same product can be viewed/update/ordered by multiple users at the same time. Hence, maintaining the history of each customer in the ThreadLocal variable will be very usefull. There we can not use Synchronization because if one user is placing the order, then the other will get blocked till the time. But in the case if we use ThreadLocal, we can easily trace the activities of each user separately. 

Thursday, June 2, 2016

Java : How to take Thread Dump in java OR usage of Jstack and VisualVM to take Thread Dump in Java

It is a very very important topic for a java developer who wants to prove to be a brilliant java developer with respect to multi-threading. It directly means that to become a multi-threading master mind you must know what is Thread Dump and how you can take Thread Dump in your project.

Suppose you are working with multi-threading and you are heavily using the synchronization in your code . Obviously, you will be using the wait(), notify() and notifyAll() methods. On the other you can also use concurrent API's for the same purpose. But while working with the synchronization in your project , it is highly recommended that your code should not cause any deadlock. Deadlock means when two threads are waiting for each other to release the lock they require.

And, if your project is using the synchronization so heavily that you are not able to find out the code which is causing the deadlock. Thread Dump is the relief for such situations. Thread Dump will give you the method and thread name which is causing the deadlock.

Thread Dump not only provides the information about the deadlock but tells the following information as well:

1) Performance Analysis: You can analyze where the application getting down and measure the performance by the help of Thread Dump.
2) Thread States: Can predict which thread is in which state(Start,Runnable,Dead etc.).
3) CPU consumption by the different different threads.
4) Garbage Collection information in detail.


Hence, this tool is very useful for the java family.

Now, we will proceed towards the procedure to take the Thread Dump. Let's move towards the cinema:

There are two ways to take the Thread Dump :
* By Using the jstack tool through command prompt (easiest way).
* By Using VisualVM tool through eclipse.

Let's go with jstack tool first:

1) Open task manager(alt+ctrl+del).
2) Go to processes tab.
3) Click on View menu and select "Select columns..." menu item.
4) Check the PID(Process Identifier) from the list and click on ok button.
5) PID would be added in the list of processes.
6) Copy the PID of javaw.exe process.
7) Open command prompt and go to the bin directory of jdk . like : C:\Program Files\Java\jdk1.7.0_79\bin
8) type the following command in command prompt:
jstack.exe -l PID     (PID is the copied PID from the task manager).
9) Press enter and Thread Dump is getting started.

You can analyse the Thread Dump generated in this way . You can also copy this Dump in any text file like with the following command:
jstack.exe -l PID > ThreadDump.txt
Where ThreadDump.txt is the file on the path : C:\Program Files\Java\jdk1.7.0_79\bin

Now, Let's go with VisualVM approach.

If we are following this approach then we need to install the VisualVM in the eclipse by using the following steps:

Download VisualVM Launcher(Eclipse 3.6+) depending on your eclipse version , extract downloaded zip file.
* Open eclipse and go to Help > Install new software
Click on Add
* Click on Local and browse the extracted launcher
* You may name it whatever you may like.Click Ok.
* Install this software like this way

Restart the Eclipse and follow these steps:

1) Go to windows > Preference > type VisualVM
2) Select visualVM executable location, Select Jdk available on your system and click OK.
3) Right click on class for which you want to run visualVm Launcher and want to analyse.
4) Go to Run As...and then select Run Configurations...
5) Select VisualVM Launcher.

Your full information about the threads causing deadlock and other information will be shown on the window opened in the eclipse. From here you can easily analyze the thread running process.

Happy Learning !!!