Tuesday, July 30

for-each loop limitations

Three scenarios where you can not use for each loop

for each loop provides a way to iterate over a collection of objects of class implementing Iterable interface. It provides little better performance as compared to traditional for loop and is a cleaner way to iterate over a collection. It removes the chances of error that could be introduced while iterating over multiple collection through traditional for loop . Developer could got wrong in updating and traversing over changing indexes of objects in collection . This kind of error is unlikely in for each loop

But there are situations where for-each can't be used

1. If you want to remove an element of collection while iterating for-each provides no remove method. So you can't use it in such scenario

2. If you want to replace some element in collection while iterating over collection You can't do that as there is no replace or update method here neither you have control to access the element with its index value .

3.A situation where you are working in parallel with multiple collections have to do a lot of operation with current index value of iterable collection. You don't have access to element index while iterating with for-each loop So you can't use it.

Semaphore in java concurrency package

What is Semaphore?

Let us understand simple concept of semaphore with an real time practical example .

Everyone at some point of time would have worked on database connection pool. In connection pool we maintain maximum number of connections allowed. So how do we control that. 

Let's say maximum number of allowed connection for a database is 10 and If 10 connection exhaust next thread will have to wait until one of the connection gets released. 

Semaphore provides a very simple way to implement this . 

Semaphore is nothing but a way of code synchronization with a specific counter. 

Let's understand this with a code snippet 

Public class ConnectionManager {

Semaphore sem =new Semaphore(10);

    public Connection getConnection(){

    public void useConnection(){

Through semaphone we maintain the counter limit for connection as 10. Any thread that needs database connection will invoke getConnection() method . 
When sem.acquire() is invoked sem counter is decreased by one . Everytime sem.acquire() method is invoked sem counter is decreased by one. So every thread that will invoke getConnection() method will decrease the sem counter by one.

When counter reaches 0 , sem.acquire() returns false and connection can not be obtained as maximum limit of allowed connection is already reached. 

Now every time sem.release() method is invoked sem counter is increased by one . This means it makes the connection available for reuse . So Client may continue trying to acquire the connection and may succeed if sem counter value become greater than 0

So semaphore helps in managing the maximum no of permits through a counter . Its value changes with critical area access acquire and release. 

That's how semaphore works. It just a better and clean way (with much lesser lines of code) to implement synchronization for situations where a counter based implementation can be useful.

Atomic variables in java

Atomic variables are introduced in java in version 5.0

To understand why atomic variables are introduced lets get through a piece of code 

Public class Calculator {
int i=0;
public int get (){
return i++;

This is pretty simple code and work as expected in single threaded environment. 

But does it behave the same way in multithreaded environment as well.

i++ is not a single step operation instead it happens in three steps

1) value to be updated is read
2) manipulation is performed
3) new value is set in variable 

So if mutiple threads call get method simultaneously they will result in operating over i in inconsistent state . one thread might be in progress of incrementing the value and at same time other thread might access the variable and perform increment on same value . So multi step operation results in highly inconsistent data state .

How do we resolve this problem ?

Way to resolve this problem is to allow only one thread at a time to access get methods and perform all three steps completely only then allow other thread to access the method. 

How do we achieve that ? 

Way to achieve that is synchronization. 

public int synchronized get (){
return i++;

This will allow only one thread to access method at one time . Thread will take the lock on the object and will release lock while exiting the method . then other thread will take the lock. 

Here multiple threads are working in sequential manner. Although data inconsistency issue is resolved But it has degraded the performance a lot . Now multiple threads are taking as much time as many number of threads execute the get() method.

Way to overcome that is atomic variables . 

 JVM compiles these classes with the better operations provided by the hardware machine, CAS or a Java implementation of the operation using a lock. 

Atomic classes in java are 

  • AtomicInteger
  • AtomicLong
  • AtomicBoolean
  • AtomicReference

All these classes supports compare-and-set (via the compareAndSet() method) . The setters operations are implemented using compareAndSet. These classes supports multi-threaded access and have a better scalability than synchronizing  the operations.

what is compareAndSet?

Quickly read that here 


Here is how we can rewrite our get() method using an AtomicInteger :

public class Calculator{

    private final AtomicInteger var= new AtomicInteger(0);
          public int get(){
            return  var.incrementAndGet();

The incrementAndGet()  method is  provided by the AtomicLong and AtomicInteger classes. there are many other methods for other operations like decrementandget() , getandset();

This is faster than the synchronized one and is also thread safe.

Monday, July 29

What is reifiable and non-reifiable java

What is reifiable and non-reifiable java ?

a reifiable type is one whose runtime representation contains same information than its compile-time representa-tion

a non-reifiable type is one whose runtime representation contains less information than its compile-time representa-tion

Arrays are reifiable as arrays remains as it is at runtime While generic information attached with List is erased at runtime by erasures

So List<String> list=new ArrayList<String>

at runtime will be

List list=new ArrayList();

all generic information is erased. This is done to support the legacy code that is written without using generics.

But in case of arrays

Object[] ojb=new Object[0]

will remain the same at runtime as well. Generics are not mixed with arrays.


                             PREFER LISTS TO ARRAYS


Lists, if implemented correctly, can save us from ClassCastException but arrays can not.

How does that happen ?

lets start it with a statement

List in invariant while arrays are covariant .

Now what is this invariant covariant difference

read the difference quickly here


Let's go through a simple example

Let's declare and instantiate an array

Object[] objectArray = new Integer[1];

assign a string value to first place in array

objectArray[0] = "str";

What happens here. This does not fail at compile time although it will throw arrayStoreException at runtime

Now let us declare and instantiate a list

List<Object> ol = new ArrayList<Integer>();

assign a string value to first place in list

what happens here. It won't compile at all. Compiler will complain You can not store String in Integer type list. So it is providing opportunity to amend the program at compile time itself instead of having a surprised burst at runtime.

Conclusively Arrays provide runtime type safety while List provides compile time type safety and we always wish to identify the error at earliest opportunity. So List should be preferred over Arrays from type safety perspective although on performance scale there can be different views depending upon the specific scenario and uage in code.

What is compare and swap and compare and set (CAS)

What is compare and swap (CAS) and compare and set(CAS) in java?

In multithreaded java programming when a shared resource is accessed by multiple threads concurrently shared data needs to be synchronized for access and  visibility. But synchronization enforce locking and block all the threads but one trying to access the resource  This is optimistic approach of enforcing single access to shared resource at one time but it results huge degradation in multithreaded environment 

So there is one optimistic approach that is CAS - compare and swap

compare and swap?

What happens here is that a thread approach a shared resource with three variables 

a) current expected value
b) new value
c) memory address of the resource

So thread will access data from memory address will compare the current value with current expected value . If they match It will update the variable to new value. Obviously If multiple threads try making this operation simultaneously only one thread will succeed and others will fail. But other threads will not block instead invoker can continue with other operation or try the same again with failing threads. So this provides a lot better performance

and what is Compare and Set ?

Compare and Set is same as compare and swap . Only difference is that it returns a Boolean value as well which determines if operation succeeded or not. 

Top Java interview questions

Coming soon...  top 99 interview questions in java j2ee

ExecutorService in java concurrency package

What is ExecutorService in java?

Executorservice is nothing but a threadpool. Threadpool is nothing but a pool of threads. Threads are nothing but Runnable or callable tasks. Runnable task is nothing but an instance of class that implements Runnable interface. Callable task is nothing but an instance of class that implements callable interface

For example :

// runnable task
public class Drive implements Runnable {
    public void run(){

//callable task
public class Drag implements Callable{
     public string call(){

Now If you want to run a thread How would you do that . Simple traditional way to do that is :

Public class tester{
   public static void main(string args[]){
       new Drive().start();

Here we are executing just one thread. 

What will we do If we have to get 10 task done in parallel and for that we require to execute more than 1 thread in parallel?

For such kind of requirements threadpool is very helpful and provide very simple mechanism to execute multiple threads in parallel.

Executorservice exec=Executors.newFixedThreadPool(8);

There are multiple implementations of ExecutorService available.

  •  Single thread executor   // only one thread in the pool
  •  cached thread Pool        // cache the thread created once. terminate the thread if thread remains unused in cache for 60 seconds
  •  Fixed Thread Pool          // fixed count of thread
  • Scheduled Thread Pool  // to schedule the future task
  • Single Thread Scheduled Pool  //only one thread to schedule future task

Now if you want to submit multiple task simply what you can do is :

ExecutorService pool= Executors.newFixedThreadPool(10);
      for (int i=0;i<20;i++){
            pool.submit(new Drag().start());

Once this task is done you can terminate this threadpool just by invoking shutdown() method:


We can implment any complex logic using ExecutorSerive and the code that we need to write is very simple . Also Executor service provide in built mechanism to control the overall execution process. 

We can also use Future interface to get the return value from each thread execution and then caculate the all Future values returnd. 

In above example we could retrieve the all future results by using below code :

List<Future<String>> futures = new ArrayList<Future<String>>(10);
     for(int i = 0; i < 10; i++){
futures.add(pool.submit(new Drag()));
    for(Future<String> future : futures){
String result = future.get();

   //Compute the result

And you can imagine what complex and error prone code you would have written by using existing API (pre- 1.5 version).

Item 22: Favor static member classes over nonstatic

             Favor static member classes over non static

What is static member class?

A nested class declared with static keyword

A non-static class has no static keyword with it .

What is major difference?

Non- static member class can't be referred standalone. It can be accessed with enclosing class only.
So when an instance of non-static class is created association with its enclosing class it created itself

And that cost extra time of instance initiation and also extra memory space for association. This association may not be required at all.

So one should always prefer static member class As that does not create any association with enclosing class.

So non-static member class should be created only when association with enclosing class is actually required i.e. there is some dependency on other part of the class as well along with member class Otherwise static member class is enough.

That saves not only time but also space So deliver great performance.

All about Anonymous Class

What is Anonymous class?

A class having no name is anonymous class. It falls in the family of nested classes in java. It is also called inner class

How is it different from a normal class?

1. It is not a member of enclosing class
2. Wherever it has to be used it is declared and instantiated there itself.
3. These classes can't extend any class and can't implement any interface.

What is the Use of Inner class?

You want to create a class on the fly. For example

to create comparators in a class where to want to sort objects list based on certain object properties

to create process objects such as Thread ,TimerTask,Runnable

These are very short lived and useful where we don't want classes or instances to live for long and wish to quickly use that and remove .

Sunday, July 28

Item 16 : Favor composition ovr inheritance

                 Favor composition over inheritance

How inheritance can be dangerous ?

Lets take an example

 public class X{

   public void do(){



Public Class Y extends X{

   public void work(){




1. As clear in above code , Class Y has very strong coupling with class X. If anything changes in superclass X , Y may break dramatically. Suppose In future class X implements a method work with below signature

public int work(){

Change is done in class X but it will make class Y uncompilable. SO this kind of dependency can go up to any level and it can be vary dangerous. Every time superclass might not have full visibility to code inside all its subclasses and subclass may be keep noticing what is happening in suerclass all the time. So we need to avoid this strong and unnecessary coupling.

How does composition solves this issue?

Lets see by revising the same example

public class X{

public void do(){



Public Class Y{
X x=new X();

public void work(){




Here we are creating reference of X class in Y class and invoking method of X class by creating an instance of X class.
Now all that strong coupling is gone. Superclass and subclass are highly independent of each other now. Classes can freely make changes which were dangerous in inheritance situation.

2. Second very good advantage of composition in that It provides method calling flexibility For example

class X implements R
class Y implements R

public class Test{

R r;


In Test class using r reference I can invoke methods of X class as well as Y class. This flexibility was never there in inheritance

3. Another great advantage : Unit testing

public class X{

public void do(){



Public Class Y{
X x=new X();

public void work(){




In above example If state of x instance is not known ,it can easily be mocked up by using some test data and all methods can be easily tested. This was not possible at all in inheritance As you were heavily dependent on superclass to get the state of instance and execute any method.

4. Another good reason why we should avoid inheritance is that Java does not support multiple inheritance.

Lets take an example to understand this :

Class Deposit implements Banking{

 boolean deposit(){

class Credit implements Banking{
boolean credit(){
Public class Transaction {


In class Transaction , for complete transaction I have to invoke both deposit as well as credit method. I can't inherit both the classes to achieve this . But I can easily use composition to make it happen simply by doing this :

Public class Transaction {
Banking b;
public static void main(String a[])


  b=new Deposit();


     b=new Credit();


Good to know : 

        1. composition is easily achieved at runtime while inheritance provides its features at                      compile time 

        2. composition is also know as HAS-A relation and inheritance is also known as IS-A                     relation

So make it a habit of always preferring composition over inheritance for various above reasons.

Monday, July 22

What is covarient and invarient

Covarient simply means if x is subtype of Y then x[] will also be sub type of Y[]. Arrays are covarient
As string is subtype of Object So

String[] is subtype of Object[]

Invarient simply means irrespective of X being subtype of Y or not , List<X> will not be subType ofList<Y>.

generics are invarient

Although string is subtype of Object So

List<String> is not subtype of List<Object>

Out Of Memory issue in java -reason ,resolution ,monitoring - what and how?

1. Analyze heap dump. 

2. Use jVisualVM to figure out the biggest object and analyze if that size could be reduced.

3. Find out objects that are unnecessarily referenced for long time and that can be dereferenced to free some memory.

4. Increase the heap size If memory allocation is not sufficient 

5. Don't keep the variables referenced for long time If they are not required all the time . Better dynamically load them whenever they are needed instead of keeping them active in memory for a very long time.

6. Avoid static references If not required. These stay in the memory till class is finally unloaded Thus keep control of lot of memory space Which might not be required all the time. Program your program intelligently.

7. Tools like ZYourKit ,Jmemter,verbos GC log,JMX monitoring,Jconsole are very good to analyze the culprit responsible for out of memory occurrence .

8.Monitor the memory usage with load factor and time factor . If memory consumption is increasing due to increase in load ,it is not memory leakage problem Instead it is memory requirement by increased load . If memory consumption is increasing with time on same load It is surely memory leak and here you definitely need to resolve it otherwise later all memory will be consumed ans System will throw OutOfMemory error.

9. jmap in jdk 1.6 is excellent tool to analyze heap dump. Heap dump gives a very clear picture of objects and dependencies circulated in the memory 

10. Setting FISHEYE_OPTS to right value is important .

FISHEYE_OPTS="-Xms128m -Xmx1024m -XX:MaxPermSize=256m"  -- Java Heap space problem

FISHEYE_OPTS="-Xms128m -Xmx512m -XX:MaxPermSize=256m"   --- Perm Generation space issue

FISHEYE_OPTS="-Xms128m -Xmx1024m -XX:MaxPermSize=256m -Xss512k" -- new thread creation space issue

What is reordering in java ?

What is reordering in java ?

When you write a program you expect instructions will be executed in same order as written in the program . i.e. execution order will remain same as the order of instructions written in the program. 

But that is not the case 

Compiler is free to execute the instructions in order it desire . For Example :

Public class Order {

int a=8;
int b=6;


Here compiler might execute b=6 prior to executing a=8 . Thus the values for a and be might get flushed as b=6 and a=0 (default value) and a thread reading values at that point of time might read value as b=6 and a=0. 

Compiler is absolutely free to conspire and reorder the execution flow . 

Why does compiler do this?

Compiler do this to achieve overall optimization . Here a and b are independent of each other So compiler might think it hardly matters of a=8 executed first or b=6 executed first . But for overall optimization executing b-6 and delaying execution of a=8 provides better performance. 

Based on latest java memory model , compiler follow reordering more seriously for better performance and optimization . 

There are techniques available in Java to restrict this ordering. Synchronization is one of the finest example of applying that. 

Synchronized HashMap versus Concurrent hashMap

1. Synchronized Map locks the entire Map While concurrent HashMap work on stripped lock principal. It divided the hash bucket in 16 different sub parts. So if one thread is reading one part of the bucket and other thread is writing on a different part , these execution will not be halted. Thus it gives better performance.

2. Concurrent HashMap does not perform well in case size() or contains() methods are invoked.As it requires synchronization on entire hash bucket.

3. If we need to make two operation on synchronized hashMap , calling code needs to be synchronized While same is not required in case of concurrent hashmap.

For example :

if(!synchronizedList.contains(element)){ ---------------------- (1)

     synchronizedList.add(element); -------------------------------(2)


this code is quiet common but not thread safe at all. 1 and 2 are synchronized but the time between the call to 1 and then to 2 there is a possibility that thread contention can occur and our thread which did the contains call may change state. To fix this we need to make this block synchronized in our code. Similar issues happen with synchronized map too.






ConcurrentHashMap solves ths problem by providing atomic putIfAbscent method calls which essentially does this job only.


4. Synchronized hashMap is fail safe So we need to exclusively synchronize to avoid concurrent modification exception. This will affect overall performance. Concurrent HashMap is lenient on this . It allows to iterate over the map even while are part of bucket is being updated. 


Item 24 : Eliminate unchecked warnings

What is unchecked warning ?

While you compile a piece of code , It is possible that It compile with warnings.You may completely ignore these warnings to get your code compiled.
What is the threat in ignoring warnings?

ClassCast Exception. Warning suggests you How you are leaving your code with risk of ClassCast exception.So It is must to resolve all warnings.
Does @suppresswarning removes the risk?

Not at all. It makes the situation more dangerous.If you add @supprresswarning on method level to avoid a specific warning It will curb all warnings that will ever come on any line of code in your method Even if those lines are added in furuture.
So you are making your code less confident and unsafe.
So what's the remedy?

1. Resolve all warnings. (recommended)

2. Reduce the scope of @suppresswarning (if you apply that ). It should be minimum, best is to use it to jusr suppress the warnings only on one line of code On which it is applied. There you get chance to resolve warnings coming in otehr part of method or class. Never use it on class or method level unless it has just one line of code.

So to make your code completely free from risk of ClassCast Exception eliminate all unchecked warnings. Otherwise keep the scope of warning supprression to minimum

Sunday, July 21

Item 23 : Don't use raw type in new code

What is new code?

Code written using release 1.5 onwards in new code. Generics are included in release 1.5
Why should we switch to generic type?

Generic types provide compile time type safety and it's good in programming to know the error as early as possible.
ClassCast exceptions that generally are unraveled only at runtime type would know detected at compile time itself if Generic types are followed.

For example :

1. List list ; // List of Horse.

2. list.add(new horse());
3. list.add(new Dog());

4. while(list.hasNext()){

5. Horse horse = list.next();

6. }

Line 5 , will throw ClassCasException as It tries to assign an instance of Dog class to Horse Type.

list is just the list of horses but mistakenly Dog is also added to that. Compiler did not compliant while adding Dog as the type of list is not specified . It's raw type list. Now the mistake skipped by Compiler costs heavily at runtime and program failed due to ClassCast Exception.

Now the Question Is Could that be avoided. Could we smell the the error earlier?

Yes , we could ask the compiler to detect the menace just by using generic type list.

1. List<Horse> list ; // List of Horse.

2. list.add(new horse());
3. list.add(new Dog());

4. while(list.hasNext()){

5. Horse horse = list.next();

6. }

at Line no 1 , list is defined to be Horse type So compiler already knows the type of list . It would complain as soon as you add Dog to the list.

Thus using generic type enforce the type safety check at compile time itself and there It can be corrected and heavy runtime burst is avoided.

Friday, July 19

Item 71: Use lazy initialization judiciously

What is lazy initialization ?

 How is it different from normal initialization?

Lazy initialization adds delay to the time an instance or static variable gets initialized.

 How much delay?

Initialization can be delayed as long as it is not required in our execution.

Why would you delay that ?

There might be a possibility that instance might not be required at all. In that case there is not only a performance gain but also you are avoiding allocating memory unnecessarily.

Then Why do you need to be Judicious? Why can't we consider it as a default principal to use lazy initialization in every case.

Lets understand that with a code snippet

Public class Car {

        private Tyre tyre ;

     public Tyre getTyreInstance(){


                 tyre=new Tyre()

              return tyre;


What is happening here. First we are checking If instance is null or not . So this execution of If statement is happening in every case . If there are too many instances required tyre are required multiple execution of this if check may deteriorates the overall performance instead of increasing depending upon how much complexity is involved in instance initialization. On the other hand if there are only a few instances of tyre are required , performance save on part of lazy initialization may be much more than that of performance loss in if statement execution.

So we need to be highly judicious while using lazy initialization.

Now how will we decide practically whether to use it or not in a given scenario. Simple answer is measure the performance with and without lazy initialization ,compare and use the option resulting in overall better performance.

Effective java Simplified

Thursday, July 11

Effective java : Item 66 : Synchronized access to shared mutable data

                  Synchronized access to shared mutable data

What is Mutable Data? : data that can be modified.

Shared ?: Being accessed and updated by multiple threads

What is Synchronized access? :

Synchronization is a keyword used  with a method or block of code to restrict the access of shared data to only one thread at one time. Essentially it provides locking mechanism .

But , Is synchronization all about locking and restriction. NO

Another very important behavior Synchronization provides is visibility. 

What is visibility ?

When a thread makes a change to shared data  this change is not always visible to other threads working on same data If shared data is not synchronized

Let’s take an example :

Public class Climate {

Boolean stop;

Thread rain =new Thread(new Runnable (

Public void run(){








Here there are two threads .
 1. Main thread
 2) rain thread spawned from main thread.  

 You may think that rain thread will execute once or twice and will stop as soon as  stop=true is executed in main thread. But this may not be the case every time , in every environment .Instead rain thread keeps working on its cached data and any change in the data does not become visible to rain thread unless it is explicitly brought in sync. 

                     So synchronization bring every participating thread in sync. To make this program run as expected every time in every environment value of stop variable should be accessed /mutated with in a synchronize block.

Write need to be synchronized for mutually exclusive access to shared data AND read needs to be synchronized so that any change in the data from any thread is visible to the thread currently reading it .

So Synchronization is important for two very strong features in multi - threaded programming

  •      Locking
  •    Visibility

 Please comment/question to discuss it further: