Sunday, May 22, 2016

Java Concurrency : CountdownLatch CyclicBarrier and Exchanger

Hi folks ,

In this example , I will be talking about three important synchronization classes CountdownLatch , CyclicBarrier and Exchanger with examples and when they are used .

-> CountdownLatch [CDL]

This allows one or more threads to wait until a set of operations being performed in other threads completes. e.g. if a thread has to wait on other dependent threads that have started , to finish .

A CDL is initialized with a given count. Each thread on whose completion the other thread is waiting , should call the countDown() method and decrease the count . Once all such threads have completed execution , the count becomes 0 and the waiting thread can proceed to perform its actions .

The await methods block until the current count reaches zero due to invocations of the countDown() method, after which all waiting threads are released and any subsequent invocations of await return immediately. This is a one-shot phenomenon -- the count cannot be reset.

A CDL initialized with a count of one serves as a simple on/off latch or gate: all threads invoking await wait at the gate until it is opened by a thread invoking countDown().

A CDL initialized to N can be used to make one thread wait until N threads have completed some action, or some action has been completed N times.

Exercise : 

The below exercise show five threads running , Main thread , worker 1 , worker 2 , waiter 1 and waiter 2 . It creates a countDownLatch of 2 counts , passes it to all the threads . The Main , Waiter 1 and Waiter 2 threads are waiting on Worker 1 and Worker 2 threads to finish , and once the worker threads finish execution , the waiter threads and the main thread are back to execution .

-------------------------------------------------------------------------
package concurrent;

import java.util.concurrent.CountDownLatch;

public class Controller {

    public static void main(String[] args) {
        final CountDownLatch latch = new CountDownLatch(2);
        new Thread(new Worker1(latch) , "Worker 1").start();
        new Thread(new Worker2(latch), "Worker 2").start();
        new Thread(new Waiter1(latch) , "Waiter 1").start();
        new Thread(new Waiter2(latch), "Waiter 2").start();
        try {
            System.out.println(Thread.currentThread().getName() + " is waiting");
            latch.await();
            System.out.println(Thread.currentThread().getName() + " is back to execution");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
-------------------------------------------------------------------------
package concurrent;

import java.util.concurrent.CountDownLatch;

public class Worker1 implements Runnable {

    private CountDownLatch latch;

    public Worker1(CountDownLatch latch) {
        this.latch = latch;
    }

    @Override
    public void run() {
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Worker 1 finished");
        latch.countDown();
    }
}
-------------------------------------------------------------------------
package concurrent;

import java.util.concurrent.CountDownLatch;

public class Worker2 implements Runnable {

    private CountDownLatch latch;

    public Worker2(CountDownLatch latch) {
        this.latch = latch;
    }

    @Override
    public void run() {
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Worker 2 finished");
        latch.countDown();
    }
}
-------------------------------------------------------------------------
package concurrent;

import java.util.concurrent.CountDownLatch;

public class Waiter1 implements Runnable {

    private CountDownLatch latch;

    public Waiter1(CountDownLatch latch) {
        this.latch = latch;
    }

    @Override
    public void run() {
        try {
            System.out.println(Thread.currentThread().getName() + " is waiting");
            latch.await();
            System.out.println(Thread.currentThread().getName() + " is back to execution");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Waiter 1 finished");
    }
}
-------------------------------------------------------------------------
package concurrent;

import java.util.concurrent.CountDownLatch;

public class Waiter2 implements Runnable {

    private CountDownLatch latch;

    public Waiter2(CountDownLatch latch) {
        this.latch = latch;
    }

    @Override
    public void run() {
        try {
            System.out.println(Thread.currentThread().getName() + " is waiting");
            latch.await();
            System.out.println(Thread.currentThread().getName() + " is back to execution");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Waiter 2 finished");
    }
}
-------------------------------------------------------------------------

Console Output :

Waiter 1 is waiting
main is waiting
Waiter 2 is waiting

3 seconds Gap

Worker 1 finished
Worker 2 finished
Waiter 1 is back to execution
Waiter 1 finished
main is back to execution
Waiter 2 is back to execution
Waiter 2 finished

Please run this program and see for yourself how CountDownLatch works.

-> CyclicBarrier  [CB]

CountdownLatch is a one-shot phenomenon -- the count cannot be reset. If you want to reset the count, use a CyclicBarrier.

A CB allows a set of threads to all wait for each other to reach a common barrier point. CB's are useful in programs involving a fixed sized party of threads that must occasionally wait for each other. It means that all the threads reaching the barrier point will have to wait for the other threads to reach the same point. As soon as all the threads reach this point , they are released to continue .

We can reuse CB by calling reset() method which resets the barrier to its initial state and hence its different from a CDL .

Used in one time events like application / module startup , waiting for individual threads to finish and then resume , used in multiplayer gaming systems where the main thread should wait till all the  players join the game .

Exercise :

The below exercise shows 3 players multi player game started from a gamin console , the gaming console is the main thread and the three players are three independent threads , each takes their own time to join the game , once a player joins the game it waits for the other players to join the game , so does the gaming console . Once all the players have joined , all the threads waiting on the barrier are released and the game starts .

package concurrent;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class GamingConsole {

    public static void main(String[] args) {
        final CyclicBarrier barrier = new CyclicBarrier(4);
        new Thread(new Player1(barrier), "Player 1").start();
        new Thread(new Player2(barrier), "Player 2").start();
        new Thread(new Player3(barrier), "Player 3").start();
        try {
            System.out.println(Thread.currentThread().getName()
                    + "Gaming Console is waiting for Players to join");
            try {
                barrier.await();
            } catch (BrokenBarrierException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()
                    + "All players have joined : Let's start");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
-------------------------------------------------------------------------
package concurrent;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class Player1 implements Runnable {

    private CyclicBarrier barrier;

    public Player1(CyclicBarrier barrier) {
        this.barrier = barrier;
    }

    @Override
    public void run() {
        try {
            System.out.println("Player 1 taking 5 seconds to join the game");
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Player 1 Joined the game : Waiting for others");
        try {
            barrier.await();
        } catch (InterruptedException | BrokenBarrierException e) {
            e.printStackTrace();
        }
        System.out.println("Player 1 plays");
    }
}
-------------------------------------------------------------------------
package concurrent;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class Player2 implements Runnable {

    private CyclicBarrier barrier;

    public Player2(CyclicBarrier barrier) {
        this.barrier = barrier;
    }

    @Override
    public void run() {
        try {
            System.out.println("Player 2 8 seconds to join the game");
            Thread.sleep(8000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Player 2 Joined the game : Waiting for others");
        try {
            barrier.await();
        } catch (InterruptedException | BrokenBarrierException e) {
            e.printStackTrace();
        }
        System.out.println("Player 2 plays");
    }
}
-------------------------------------------------------------------------
package concurrent;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class Player3 implements Runnable {

    private CyclicBarrier barrier;

    public Player3(CyclicBarrier barrier) {
        this.barrier = barrier;
    }

    @Override
    public void run() {
        try {
            System.out.println("Player 3 taking 2 seconds to join the game");
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Player 3 Joined the game : Waiting for others");
        try {
            barrier.await();
        } catch (InterruptedException | BrokenBarrierException e) {
            e.printStackTrace();
        }
        System.out.println("Player 3 plays");
    }
}
-------------------------------------------------------------------------

Console Output :

Player 1 taking 5 seconds to join the game
Player 2 8 seconds to join the game
Player 3 taking 2 seconds to join the game
main Gaming Console is waiting for Players to join
Player 3 Joined the game : Waiting for others
Player 1 Joined the game : Waiting for others
Player 2 Joined the game : Waiting for others
Player 2 plays
main All players have joined : Let's start
Player 3 plays
Player 1 plays

The CB uses an all-or-none breakage model for failed synchronization attempts: If a thread leaves a barrier point prematurely because of interruption, failure, or timeout, all other threads waiting at that barrier point will also leave abnormally via BrokenBarrierException (or InterruptedException if they too were interrupted at about the same time).

-> Exchanger

A synchronization point at which threads can pair and swap elements within pairs. Each thread presents some object on entry to the exchange method, matches with a partner thread, and receives its partner's object on return.
Whenever a thread arrives at the exchange point, it must wait for the other thread to arrive. When the other pairing thread arrives the two threads proceed to exchange their objects.

Exercise :

The following example shows a Money exchange system with bills and coins . Two candidates want to exchange their money and use this system to exchange their money . The candidate 1 arrives earlier than candidate 2 to exchange money , however waits for Candidate2 to arrive and exchange.

package concurrent;

import java.util.concurrent.Exchanger;

public class ExchangerImpl {

    public static void main(String[] args) {
        final Exchanger<Money> exchanger = new Exchanger<Money>();
        Money candidate1 = new Money(100, 30);
        Money candidate2 = new Money(75, 25);
        new Thread(new Candidate1(candidate1, exchanger), "Candidate 1")
                .start();
        new Thread(new Candidate2(candidate2, exchanger), "Candidate 2")
                .start();
    }
}
-------------------------------------------------------------------------
package concurrent;

import java.util.concurrent.Exchanger;

public class Candidate1 implements Runnable {

    private Exchanger<Money> exchanger;
    private Money money;

    public Candidate1(Money money, Exchanger<Money> exchanger) {
        this.money = money;
        this.exchanger = exchanger;
    }

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() + " : Before :"
                + money.getBill() + "." + money.getCoin());
        try {
            System.out.println("Candidate 1 is ready to exchange money : waiting");
            money = exchanger.exchange(money);
            System.out.println("Candidate 1 has exchanged money");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + " : After :"
                + money.getBill() + "." + money.getCoin());
    }
}
-------------------------------------------------------------------------
package concurrent;

import java.util.concurrent.Exchanger;

public class Candidate2 implements Runnable {

    private Exchanger<Money> exchanger;
    private Money money;

    public Candidate2(Money money, Exchanger<Money> exchanger) {
        this.money = money;
        this.exchanger = exchanger;
    }

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() + " : Before :"
                + money.getBill() + "." + money.getCoin());
        try {
            System.out.println("Candidate 2 needs 5 seconds to arrive");
            Thread.sleep(5000);
            money = exchanger.exchange(money);
            System.out.println("Candidate 2 has exchanged money");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + " : After :"
                + money.getBill() + "." + money.getCoin());
    }
}
-------------------------------------------------------------------------

Console output :

Candidate 1 : Before :100.30
Candidate 1 is ready to exchange money : waiting
Candidate 2 : Before :75.25
Candidate 2 needs 5 seconds to arrive
-------------------5 seconds-------------
Candidate 2 has exchanged money
Candidate 2 : After :100.30
Candidate 1 has exchanged money
Candidate 1 : After :75.25





Saturday, May 21, 2016

Singleton - Master it in 10 mins .

Hi folks ,

Let's have some understanding on basic design pattern in any programming model known as the Singleton .This session is based on Singleton in Java. I hope you enjoy reading it and make the most out of it .

Singleton design pattern is one of the most common patterns you will see in Java applications and it’s also used heavily in core Java libraries. In this post i have written about almost everything i know and what possibly is out about Singleton.

Audience : If you are a Java beginner or an experienced person , this is for you . If you are an interviewer you can use this . If you want to prepare for an interview definitely read it .


  • What is singleton : Singleton is a class which has only one instance in whole application and provides a getInstance() method to access the singleton instance.
  • Any class which you want to be available to whole application and only one instance is needed in the lifetime of the application. The best example would be Runtime class ,  since on whole java application only one runtime environment can be possible making Runtime Singleton is right decision.  
Ways to write a singleton -

A). Lazy loading : Only when the client asks for the object , create and return it, otherwise don't bother.

public class Singleton {
    private static Singleton INSTANCE;

    public static Singleton getInstance() {
            if (INSTANCE == null) {
                INSTANCE = new Singleton();
            }
            return INSTANCE;
    }
}

B). Early loading : Here , Singleton instance is created when class is loaded into memory.

public class Singleton {
    private static final Singleton INSTANCE = new Singleton();

    public static Singleton getInstance() {
            return INSTANCE;
    }
}

C). When your application runs in a multi threaded environment the above code will create multiple instances of Singleton class , hence make this whole getInstance() method synchronized .

public class Singleton {
    private static Singleton INSTANCE;

    public static synchronized Singleton getInstance() {
        if (INSTANCE == null) {
            INSTANCE = new Singleton();
        }
        return INSTANCE;
    }
}


D). Though the above way is  thread-safe and solves issue of multiple instances , it's not very efficient. One that you need to bear cost of synchronization the entire time this method is called  even when  synchronization is only needed when Singleton instance is created.

We have the double checked locking pattern, where only critical section of code is locked. It's called double checked locking because there are two checks for INSTANCE == null , one without locking and other with locking (inside synchronized) block.

public class Singleton {
    private static Singleton INSTANCE;

    public static Singleton getInstance() {
        if (INSTANCE == null) { // single check
            synchronized (Singleton.class) {
                if (INSTANCE == null) { // double check
                    INSTANCE = new Singleton();
                }
            }
        }
        return INSTANCE;
    }
}

Double checked locking should only be used when you have requirement for lazy initialization otherwise use Enum based Singleton .

In the above process unntil you make INSTANCE variable volatile , the code is broken !! WHY ???

Without volatile modifier it's possible for another thread in Java to see the half initialized state of INSTANCE variable, but the volatile keyword assures the happens-before relationship, all the writes will happen on volatile INSTANCE before any of its reads .


    private volatile static Singleton INSTANCE;

E). Enum Singletons are new way to implement Singleton pattern in Java by using Enum with just one instance.  Compared to double checked locking with synchronization , Enum singletons are cake walk ascreating of Enum instance is thread-safe by default.Lesser code and all you need to do is EasySingleton.INSTANCE to get the instance .


public enum Singleton {
    INSTANCE;
}

F). Initialize-on-demand :  This method relies on the JVM only initializing the class members on the first reference to the class. In this case, we have a inner class that is only referenced within the getInstance() method , initializing the INSTANCE.

public class Singleton {

    public static Singleton getInstance() {
        return SingletonHolder.INSTANCE;
    }

    private static class SingletonHolder {
        static final Singleton INSTANCE = new Singleton();
    }
}

Now let's see how can singleton contract be broken and how can we avoid that.


  • Clone method : Do not implement Cloneable interface, if do so , throw an Exception from clone method with a message like Singleton already created .
  • Constructor / Reflection : Even though we can keep the constructor as private ,via reflection its possible to access the private methods and variables. Ensure to throw an exception if constructor gets called , as Singleton already cerated.  
  • Serialization : During serialization readObject() is used to create instance of the  class and it return new instance every time but by using readResolve() method, one can replace it with original Singleton instance. 

Now that's all about our old and famous Singleton design pattern  . I hope you learnt something new today and hopefully will not forget these points.

Happy Coding.

Singly LinkedList implementation in Java from scratch

Hi Folks ,

Please find  below the implementation of Singly Linked List from scratch in Java.

This is my way of implementing it , if you found any errors or improvements , i am welcome to any comments .

package LinkedListImpl;

public class Link {

    Integer data;
    Link link;

    public Link(Integer data) {
        this.data = data;
    }

    public Link() {

    }

    public void addFirst(Integer data) {
        int tempData = this.data;
        Link next = this.link;
        this.data = data;
        Link newLink = new Link(tempData);
        this.link = newLink;
        newLink.link = next;
    }

    public void add(Integer index, Integer data) {
        if (this.data == null) {
            this.data = data;
        } else {
            Link prev = this;
            while (index != 1) {
                prev = prev.link;
                index--;
            }
            int tempData = prev.data;
            Link rest = prev.link;
            prev.data = data;
            Link newLink = new Link(tempData);
            prev.link = newLink;
            newLink.link = rest;
        }
    }

    public void addLast(Integer data) {
        if (this.data == null) {
            this.data = data;
        } else {
            Link prev = this;
            while (prev != null && prev.link != null) {
                prev = prev.link;
            }
            Link newLink = new Link(data);
            prev.link = newLink;
        }
    }

    public boolean contains(Integer data) {
        Link cur = this;
        while (cur != null) {
            if (cur.data == data) {
                return true;
            }
            cur = cur.link;
        }
        return false;
    }

    public void set(Integer index, Integer data) {
        Link cur = this;
        int cnt = 1;
        while (cnt != index) {
            cur = cur.link;
            cnt++;
        }
        cur.data = data;
    }

    public Integer getFirst() {
        return this.data;
    }

    public Integer get(Integer index) {
        Link cur = this;
        int cnt = 1;
        while (cnt != index) {
            cur = cur.link;
            cnt++;
        }
        return cur.data;
    }

    public Integer getLast() {
        Link cur = this;
        while (cur.link != null) {
            cur = cur.link;
        }
        return cur.data;
    }
   
    public Integer removeFirst() {
        if (this.link == null) {
            return this.data;
        }
        int removed = this.data;
        this.data = this.link.data;
        Link next = this.link.link;
        this.link = next;
        return removed;
    }

    public void remove(Integer data) {
        if (this.link == null) {
            removeFirst();
        } else {
            Link cur = this;
            Link prev = null;
            while (cur.link != null && cur.data != data) {
                prev = cur;
                cur = cur.link;
            }
            if(cur.link == null) {
                prev.link = null;
                cur = null;
            }
            if (cur != null) {
                cur.data = cur.link.data;
                cur.link = cur.link.link;
            }
        }
    }
   
    public Integer removeLast() {
        Link prev = null;
        Link cur = this;
        while (cur.link != null) {
            prev = cur;
            cur = cur.link;
        }
        prev.link = null;
        int removed = cur.data;
        cur = null;
        return removed;
    }

    public int size() {
        Link cur = this;
        int size = 0;
        while (cur != null) {
            cur = cur.link;
            size += 1;
        }
        return size;
    }

    public void printList() {
        Link prev = this;
        while (prev.link != null) {
            System.out.print(prev.data + " ");
            prev = prev.link;
        }
        System.out.print(prev.data + "\n");
    }

    @Override
    public String toString() {
        Link prev = this;
        StringBuffer sb = new StringBuffer();
        while (prev.link != null) {
            sb.append(prev.data);
            prev = prev.link;
        }
        sb.append(prev.data);
        return sb.toString();
    }
}



Happy Coding

Friday, May 6, 2016

Bash FAQ's

Using awk to extract lines in a text file

http://linuxcommando.blogspot.in/2008/04/using-awk-to-extract-lines-in-text-file.html

How to print the line number where a string appears in a file?

http://stackoverflow.com/questions/30658703/how-to-print-the-line-number-where-a-string-appears-in-a-file

To print a specific line from a file

http://www.commandlinefu.com/commands/view/3802/to-print-a-specific-line-from-a-file

How to remove space from string?

http://stackoverflow.com/questions/13659318/how-to-remove-space-from-string

Check space used by each folder in side a dir

du -h --max-depth=1

Logical Operations on grep -

$for I in `grep -l -r 'dir1/dir2/dir3/dir4/dir...n/*/*/*/`;
do
grep -l -w 'match1' $I && grep -l 'match2' $i;
done

#execute script remotely

check if number
[[ $1 =~ ^[-0-9]+$ ]] && echo "number"

return from function

Alternative for expr

for i in `grep  -w '\<param name=\"csvFile' Cir*.xml`; do echo $i; done | grep csv | cut -d '>' -f2 | cut -d '<' -f1 | grep -i ciru

while true; do grep -E 'Relations has|has finished|created ='  nohup.out ; sleep 10; clear; done

IFS=$'\n';for i in `grep -l  29513363 circuit*.csv cirucit_etfl_vlan_1.csv`; do echo $i &&head -1 $i && grep -w 29513363 $i; done| less

sudo ifconfig eth0 192.168.23.137
sudo route add default gw 192.168.23.2

Mounting shared folders on vmware 

http://askubuntu.com/questions/29284/how-do-i-mount-shared-folders-in-ubuntu-using-vmware-tools

https://www.linode.com/docs/networking/diagnostics/diagnosing-network-speed-with-iperf

https://linuxaria.com/article/tool-command-line-bandwidth-linux

on Windows
wmic cpu get VirtualizationFirmwareEnabled

Wednesday, April 20, 2016

How to use comparator in Java ??

package : java.util
public interface Comparator<T>
 
A comparison function that imposes a total ordering on some 
collection of objects.They can be passed to a sort method 
(Collections.sort()) | Arrays.sort()) to allow precise control
over the sort order. 
 
It provides multiple sorting sequence i.e. you can sort the
elements based on any data member. For instance it may be 
on rollno, name, age or anything else.  

Can also be used to control the order of certain data structures 
Sorted sets or Sorted maps , or to provide ordering of collections
of objects that do not have a natural ordering.
 
The ordering set by a comparator c on a set of elements S is said
to be consistent with equals id c.compare(s1,s2) == 0 is logically
same as s1.equals(s2) for every s1 , s2 in S.
 
If compare is not consistent with equals for a sorted set or a 
sorted map , the set or map behaves weirdly as the sorted set / map 
violates the contract set for a set or a map.

Method detail :

int compare(T o1 , T o2);

Compares two arguments for order .
-> o1 is less than o2 , returns negative 
-> o1 is equal to o2 , returns 0 
-> o1 is greater than o2 , returns positive.

The above is a signum function like sgn(expression) that returns 
-1 ,0 or 1 based on the sgn(expression) is negative , zero or 
positive.

Implementor should take care of the following : 
1. Ensure that sgn(compare(x, y)) == -sgn(compare(y, x)) 
for all x and y. 
2. Ensure that the relation is transitive: 
((compare(x, y)>0) && (compare(y, z)>0)) implies compare(x, z)>0.
3. Ensure that compare(x, y)==0 implies that 
sgn(compare(x, z))==sgn(compare(y, z)) for all z.

It is generally the case, but not strictly required that
(compare(x, y)==0) == (x.equals(y)).Generally speaking, any 
comparator that violates this condition should clearly indicate 
this fact. The recommended language is "Note: this comparator 
imposes orderings that are inconsistent with equals."

Exercise : 
 
package sample ;
public class City {
 private String name;
 private int number;

 City(String name, int number) {
  this.name = name;
  this.number = number;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public int getNumber() {
  return number;
 }

 public void setNumber(int number) {
  this.number = number;
 }
} 
 
The above is a model class City having two instance variables 
number and Name .
--------------------------------------------------------------
package sample;

import java.util.Comparator;

public class NumberComparator implements Comparator<City>{

 @Override
 public int compare(City o1, City o2) {
  return o1.getNumber() - o2.getNumber();
 }
} 
 
The above is a number comparator which implements the compare 
method and compares the city numbers .
----------------------------------------------------------------
package sample;

import java.util.Comparator;

public class NameComparator implements Comparator<City> {

 @Override
 public int compare(City o1, City o2) {
  return o1.getName().compareTo(o2.getName());
 }
}

The above is a name comparator which uses the Strings' compareTo 
method to compare the City based on the names.
----------------------------------------------------------------
For below class please see comments : 

package sample;

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

public class TestComparator {

 public static void main(String[] args) {
  List<City> cList = new ArrayList<City>();
  cList.add(new City("Acity", 10));
  cList.add(new City("Zity", 4));
  cList.add(new City("Fcity", -5));
  cList.add(new City("Kcity", 99));
  cList.add(new City("Rcity", 3));
  cList.add(new City("Bcity", 25));
  
  System.out.println("Name Comparator"); 
                // Sorting the list by passing the nameComparator
  Collections.sort(cList, new NameComparator());
  for (City c : cList) {
   System.out
     .println("City : " + c.getNumber() + " ). " + c.getName());
  }

  System.out.println("Number Comparator");                // Sorting the list by passing the numberComparator    
  Collections.sort(cList, new NumberComparator());
  for (City c : cList) {
   System.out
     .println("City : " + c.getNumber() + " ). " + c.getName());
  }
 } 
}
 
Output : -
  
  Name Comparator
  Name : 10 ). Acity
  Name : 25 ). Bcity
  Name : -5 ). Fcity
  Name : 99 ). Kcity
  Name : 3 ). Rcity
  Name : 4 ). Zity
 
  Number Comparator
  Name : -5 ). Fcity
  Name : 3 ). Rcity
  Name : 4 ). Zity
  Name : 10 ). Acity
  Name : 25 ). Bcity
  Name : 99 ). Kcity
 
Note: Sorting of the Arrays class is as the same as the Collections.

Hope you learnt something new today .

Happy Coding

Monday, April 18, 2016

Using The Java ThreadPoolExecutor framework

Hi folks, 

The following exercise helps you understand and implement java's Thread Pool Executor framework. A thread pool manages the pool of worker threads . It contains a queue that keeps the tasks waiting to be executed.

The most common fixed thread pool always has a specified number of threads running; if a thread is somehow terminated while it is still in use, it is automatically replaced with a new thread. Tasks are submitted to the pool via an internal queue, which holds extra tasks whenever there are more active tasks than threads.

Thread pools address two different problems:

1. They usually provide improved performance when executing large numbers of asynchronous tasks, due to reduced per-task invocation overhead.


2.They provide a means of bounding and managing the resources, including threads, consumed when executing a collection of tasks.

ThreadPoolExecutor ,  an executor service that executes each submitted task using one of possibly several pooled threads, normally configured using Executors factory methods.

Each ThreadPoolExecutor also maintains some basic statistics, such as the number of completed tasks.

However, programmers are urged to use the more convenient Executors factory methods
a).newCachedThreadPool()[unbounded thread pool, with automatic thread reclamation] ,

b).newFixedThreadpool(int) [fixed size thread pool] and

c).newSingleThreadExecutor()[single background thread].


Using A ThreadPool Executor - 

public ThreadPoolExecutor(int corePoolSize,
                  int maximumPoolSize,
                  long keepAliveTime,
                  TimeUnit unit,
                  BlockingQueue<Runnable> workQueue)

-- Creates a new ThreadPoolExecutor with the given initial parameters and default thread factory and rejected execution handler.
 



-> CorePoolSize and MaxPoolSize

 A ThreadPoolExecutor will automatically adjust the pool size according to the bounds set by corePoolSize & maximumPoolSize. When a new task is submitted  and fewer than corePoolSize threads are running, a new thread is created to handle the request, even if other worker threads are idle. If there are more than corePoolSize but less than maximumPoolSize threads running, a new thread will be created only if the queue is full. By setting corePoolSize and maximumPoolSize the same, you create a fixed-size thread pool. By setting maximumPoolSize to an essentially unbounded value such as Integer.MAX_VALUE, you allow the pool to accommodate an arbitrary number of concurrent tasks.New threads are created using a ThreadFactory.The defaultThreadFactory creates threads to all be in the same ThreadGroup and with the same NORM_PRIORITY priority and non-daemon status.By supplying a different ThreadFactory, you can alter the thread's name, thread group, priority, daemon status, etc. 

-> Rejected Tasks - 

New tasks submitted  will be rejected when the Executor has been shut down, and also when the Executor uses finite bounds for both maximum threads and work queue capacity, and is saturated.In either case, the execute method invokes RejectedExecutionHandler.rejectedExecution .

 ->Hook methods:

This class provides protected overridable beforeExecute(java.lang.Thread , java.lang.Runnable) and afterExecute(java.lang.Runnable , java.lang.Throwable) methods that are called before and after execution of each task. These can be used to manipulate the execution environment; for example, reinitializing ThreadLocals, gathering statistics, or adding log entries. Additionally, method terminated() can be overridden to perform any special processing that needs to be done once the Executor has fully terminated. 

-> Queuing:

Any BlockingQueue may be used to transfer and hold submitted tasks.If fewer than corePoolSize threads are running, the Executor always prefers adding a new thread rather than queuing.If corePoolSize or more threads are running, the Executor always prefers queuing a request rather than adding a new thread.If a request cannot be queued, a new thread is created unless this would exceed maximumPoolSize, in which case, the task will be rejected.There are three types of blocking queues : SynchronousQueue , LinkedBlockingQueue , ArrayBlockingQueue. Read more about BlockingQuere here. 

-> Keep-alive times:

If the pool currently has more than corePoolSize threads, excess threads will be terminated if they have been idle for than the keepAliveTime. This provides a means of reducing resource consumption when the pool is not being actively used. Using a value of Long.MAX_VALUE, TimeUnit.NANOSECONDS disables idle threads from ever terminating prior to shutdown.

Exercise :   

1. Let's extend the ThreadPoolExecutor by our own class -

package sample.exercise;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class SampleThreadPoolExecutor extends ThreadPoolExecutor {

public SampleThreadPoolExecutor(int corePoolSize, int maxPoolSize,
long keepAliveTime, TimeUnit timeUnit,
BlockingQueue<Runnable> blockingQueue, ThreadFactory threadFactory) {
super(corePoolSize, maxPoolSize, keepAliveTime, timeUnit,
blockingQueue, threadFactory);
}

@Override
protected void beforeExecute(Thread arg0, Runnable arg1) {
                // s.o.p before execute called
super.beforeExecute(arg0, arg1);
}

@Override
protected void afterExecute(Runnable arg0, Throwable arg1) {
                // s.o.p after execute called
super.afterExecute(arg0, arg1);
}
}

---------------------------------------------------------------------------------------------------------------------

2. Let's create a basic TaskClass class that implements run method of the Runnable interface , the object of this class is the actual task going to the blocking queue of the threadPoolExcecutor.

package sample.exercise;

public class TaskClass implements Runnable {

@Override
public void run() {
System.out.println("Write your task here");
}
}

---------------------------------------------------------------------------------------------------------------------

3.  Let's have a main method to run our executor .

package sample.exercise;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;

public class InitThreadPool {

private BlockingQueue<Runnable> blockingQueue;
private ThreadFactory threadFactory = Executors.defaultThreadFactory();
private SampleThreadPoolExecutor executor;
private int corePoolSize;
private int maxPoolSize;
private long keepAliveTime;

public InitThreadPool(BlockingQueue<Runnable> blockingQueue)
throws ConfigurationException {
// set the core poolSize this.corePoolSize = ;
// set the maxPoolSize this.maxPoolSize = ;
// set the keep alive time this.keepAliveTime = ;
TimeUnit timeUnit = TimeUnit.NANOSECONDS;
this.blockingQueue = blockingQueue;
this.executor = new SrimsThreadPoolExecutor(corePoolSize, maxPoolSize,
keepAliveTime, timeUnit, blockingQueue, threadFactory);
}

public static void main(String[] args) throws Exception {
InitThreadPool sampleThreadPool = new InitThreadPool(
new LinkedBlockingQueue<Runnable>());
for (int i = 0; i < 100; i++) {
executor.execute(new TaskClass());
}
                executor.shutdown();
                while (!executor.isTerminated()) {
                }
                System.out.println("Finished all threads");
}
}
----------------------------------------------------------------------------------------------------------------------------------

100 tasks of class TaskClass will be sent to the linkedBlocking queue for execution , meaning 100 run methods will be executed by the executor.
 

I hope you learnt something new through this exercise and your understanding of the ThreadPoolExecutor is clear now.
 

Happy Coding.