Performance analysis : Top-Down and Bottom-Up Approach

In this article we are going to see basic Performance Analysis approaches. I will be referring to Top-Down & Bottom-up approach. I will not compare them as both are used for analysis, I will only try to explain what are the basic steps and when to choose what type. This article mostly match for Java & DotNet application. You may use similar approaches for other platforms too.

Top-Down Analysis :

This is the most popular method. The idea is simple, performance monitoring application from top view. That means, client monitoring -> server monitoring in OS & Resource level, then app server-> then run time environment
And, when we get unusual behavior (or not expected or targeted), then profiling or instrument application. In this moment, we have found problems , and experiment with possible solution, choose the best one.
And then, we have to tune the system. Tune code with best solution, tune environment and tune run time.
Finally, we need to test for the impact. Typically, before starting an analysis, we should have some measurement data(or benchmarks) from performance test results. We need to retest those and compare with previous results. If that is significant, then it is done, if not, we need to get back to profiling and tuning application.

Here is a flow chart to show at a glance : Google drive link. Open with 


When we use?
-> Application causing issues, we need optimize whole or a part of application.
-> Optimize application for given resources(CPU/Disk/Memory/IO/network)
-> Tune the system & application for best performance.
-> We have access to code change, now need to tune application for specific goal.(throughput, response time, longer service etc).
-> We need Root Cause Analysis for unexpected Performance Issues(OOM, Slowness, crashing in different level or sub-systems, unwanted application behavior primary suspected for performance, etc)

Bottom-Up Analysis :
This is another popular approach when we need to tune resource or platform (or hardware) for specific application. Let say, you have a java application , deployed. Now, bottom up analysis will allow you to analyze and find optimization scope for deployed system, hardware and resources. This is very common approach for application capacity planning, benchmarking for changed environments(migration). The key idea is, monitor application in specific environment and then tune environment(software+ hardware resources) that makes target application running at top performance.

Here is a flow chart to show at a glance : Google drive link. Open with 


When we use?
-> When you need to increase performance but you cant change source code.
-> You need to optimize resources & environment for specific application( deployed environment)
-> You need to have benchmark and get to know resource usages as well as find possible area for tuning.
-> You need to optimize run time (JVM/CLR) for your application. You can see resource usages and tune as your app needs.
-> When you need capacity planning for your hardware which must run application in optimal way.
-> When you have optimized your application from code and there is not visible area to tune, you can use this technique to achieve some more. 

Please comment if you have any question.

Thanks.. :)

Java Thread & Synchronization : How to use thread?

This is continuing article of previous post. In this article, we are going to see how to implement or use thread in java.

We can apply threading in two ways.
a. Extending Thread class : Which is self executable (can run directly)
b. Implement runnable interface : Which is not self executable , need to include a thread to execute this.
Basically they are operated by same technique, if you see Thread class which is implementing runnable interface. That we will override. Let’s see those two
A. Extending Thread class :
We need to first extend from thread class. I am creating MyThread class which extends Thread

public class MyThread extends Thread{
    public MyThread(){
        this("No Name Provided");
    public MyThread(String name) {
        System.out.println("Extended Thread Created: " + name+" : Status ->"+super.getState().toString());               
    public MyThread(Runnable arunnable, String name) {
        super(arunnable, name);
        System.out.println("Extended Thread Created with runnable : " + name+" : Status ->"+super.getState().toString());           
    public void start(){// Changed start
        System.out.println("Thread started "+super.getName()+" : Status ->"+super.getState().toString());
    public void run() {// Changed run
        System.out.println("Thread is running "+super.getName()+" : Status ->"+super.getState().toString());       
In each initiation, I provide print so that we can understand how it is working. Now, lets see we make a method under main that calls my thread to run.
public static void runThreadByExtendingThread(){
        MyThread t;
        for(int i=0; i<=5; i++){
            t=new MyThread("T Name : "+i);   
            System.out.println("Thread Created, Serial = "+i+", Name :"+t.getName() +" ID : "+t.getId()+" Status : "+t.getState().toString());
            System.out.println("Thread started, Serial = "+i+", Name :"+t.getName() +" ID : "+t.getId()+" Status : "+t.getState().toString());

And  adding this to main

public static void main(String[] args) {
        }catch(Exception e)

If we run this , we can see following in console

So, from here, we can see, how status is changing for a particular thread. I pointed about initial created thread(0 th)
Benefits :
1. We can implement our own type of execution engine by overriding inherited method.
2. Full execution unit is in our control, so we can design full work flow type environment inside JVM in controlled way. This is very useful while designing your own framework or custom UI component or even custom test runner.

Weak points :
As we have to inherit, so, only our custom thread cant be inherit more.

B. Implement runnable interface :
This is so simple. We need to implement runnable and override run() method. This is our main execution engine. So, we make our own method runnable and then initiate with a thread, then the thread runs this. So, I am making my own Runnable class MyRunnable

public class MyRunnable implements Runnable{
    private static int counter=0;// to see how many time it is calling run()   
    public MyRunnable(){   
    public void run() {
        System.out.println("Runnable is running at -> runnable counter "+counter++);       

And, like as previous method, make a method to call this.
public static void runThreadByImplementRunnable(){
        MyRunnable runnable;
        Thread t2;
        for(int i=0; i<=5; i++){
        runnable = new MyRunnable();
        t2=new Thread(runnable,"T Name : "+i);
        System.out.println("Thread Created, Serial = "+i+", Name :"+t2.getName() +" ID : "+t2.getId()+" Status : "+t2.getState().toString());
        System.out.println("Name :"+t2.getName() +" ID : "+t2.getId()+" Status : "+t2.getState().toString());

And, calling this method from main 

public static void main(String[] args) {
        }catch(Exception e)

If we run this we can see following in command line

From here, we can , initial thread (oth) created and running our run() method. we can see step by steps execution .
Thread 0, created –> become runnable and then start our run() method. And between that, another created. 

Note : Thread use start() to call run(), it is better not to call run() method by our self. Why? you can see in thread class, jvm make some steps before calling run to make it safe.

Benefits :
1. This is faster way to implement
2. No barrier on extending class, so, just implementing runnable is enough.

Weak points :
We cant have control on execution environment.

In here you can see source code form github. I will add more example under this multithreading source code.

In the next article, we are going to see, multi-threading & communication among them.

Please comment if you have any question.
Thanks.. :)

Java Thread & Synchronization : Basic introduction to thread

In this article we are going to learn about Java Thread & Synchronization, basic introduction. The theory part which is necessary for applying threading & synchronization. I will try to cover minimum information needed for understanding, detail you may find in Oracle's blogs or Java code geek blogs.

What is a Thread ?
A thread is a mini/light weight processing unit used by Run time Environment(JVM) to process which are isolated among each other. Why it is lightweight, because, a thread can't be independently full but it is part of a process. So, technically, a thread is a single processing unit for a whole task/process.

To have clear idea, lets introduce some related terms.

a. Multi tasking :  When CPU preforms multiple process from single user.
b. Multi processing : When a CPU performs multiple processes from multiple user
c. Multi Threading : When multiple threads performs under a single process to a CPU.

So, by architecture, a thread will have

1. Single process ID, name (or Identifier from Operating system)
2. Single resource unit shared among all threads under a process. The resource can be memory, CPU time. 


To know memory management details , you can visit my JVM architecture post.

Note : As you know, from CPU world , there is shared CPU core technology called Hyper Threading which is core sharing technology that enables two processes running under same CPU core. Please dont confuse with this. Hyper threading makes two logical processing unit for a single core which is actually recognized by OS as two processing unit. Where as, multiple thread (2 or more) is recognized as single process to OS which is handle by its own mechanism.

How thread works?
A thread is a implementation of runnable interface with run() method. So, when a thread is created, it actually initiate an execution unit in JVM which actually runs the statement declared in run(). Usually, when we create a thread, we need to call start() to start the thread and start() actually calls run() safely.
Thread can wait, join, stop for other threads.
A group of thread can work like as, 
Preemptive multithreading : In here, CPU time shared process where each thread get time to run in CPU(queued or randomly chosen or highest priority get CPU time first). This is fully controlled by JVM. 
Cooperative multithreading : in here, Highest priority always get CPU time first. Unless , we explicitly make unselfish, it behaves like as selfish multithreading. [yield() is used with same priority to make unselfish]
Note : In any java profiler, we can actually see the running thread. For visual VM, see the thread tab.
A thread dump contains currently running thread information. 

Life cycle : A thread generally have 4 State in its life cycle.
a. Ready
b. Running
c. Hold/Sleep
d. Stop/Death of a thread

here is how, thread changes its states :

From ready to running, it is done by start() method calls, which actually runs run() method.

From running to hold, it is done by wait() or sleep(ms) & eventually back to running by either time out or notify()/ notifyall()[for wait() calls only]

Note : Before jdk 1.2, suspend() and resume() was used , but it is deprecated due to thread safe.

From running to stop or death , calling stop() which is deprecated in . It is handled by JVM it self.

Why deprecated? you can find very good explanation in stackoverflow.

Type of thread : 
User Thread : The thread which are use for user tasks and managed by users, they are user thread. Stopping of a thread is done by users.
Demon thread : The thread which are managed by JVM it self and act like as service. (example, GC). It used to be low in priority due to it’s nature. Stopping of a thread is done by JVM.
We can change thread type but we can’t change type when thread started.

Important Properties of a thread  :
a. priority: (int, 1-10), representing priority of a particular thread to JVM. Default value 5 as normal. If it is created under a thread group, it gets the value from group. And, if the group default max value changed, it changed to max.
b. Name(char[]) : representing name ,
c. ID (long): Id of a thread.
d. threadStatus(int): shows thread stated or not
e. group (ThreadGroup) : Represents a set threads with common purposes, easy to manage all together.
All are private so, we can access by getters.
In the next post , we will see, how threads can be applied in application.

Please comment if you have any question.

Thanks.. :)