Tuesday 5 May 2015

Check befire Down casting in Java

Before downcasting, check for the dynamic type of the object and then downcast.
StringBuffer str = new StringBuffer("Hello");
Object obj = str;
if(obj instanceof String)
{
String strBuf = (String) obj;
}

Daemon thread in Java

A daemon thread is a thread, that does not prevent the JVM from exiting when the program finishes but the thread is still running.
An example for a daemon thread is the garbage collection.
You can use the setDaemon() method to change the Thread daemon properties.

setDamon(boolean) can only be called before the thread has been started.
By default the thread inherits the daemon status of its parent thread.

  • When a new thread is created it inherits the daemon status of its parent.
  • Normal thread and daemon threads differ in what happens when they exit.                           When the JVM halts any remaining daemon threads are abandoned: finally blocks are not executed, stacks are not unwound - JVM just exits. Due to this reason daemon threads should be used sparingly and it is dangerous to use them for tasks that might perform any sort of I/O.

public class DaemonTest
{
  public static void main(String[] args)
  {
     new WorkerThread().start();
     try 
     {
         Thread.sleep(7500);
     }
     catch (InterruptedException e)
     {}
    System.out.println("Main Thread ending") ;
  }
}

class WorkerThread extends Thread
{
  public WorkerThread()
  {
     setDaemon(true) ; // When false, (i.e. when it's a user thread), // the Worker thread continues to run. // When true, (i.e. when it's a daemon thread), // the Worker thread terminates when the main // thread terminates.
   }
   public void run()
   {
      int count=0 ;
      while (true)
     {
        System.out.println("Hello from Worker "+count++) ;
         try
        {
           sleep(5000);
        }
        catch (InterruptedException e)
        {}
     }
  }
}

Sorting Techniques use in Java

According to the Java 7 API doc for primitives:
Implementation note: The sorting algorithm is a Dual-Pivot Quicksort by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm offers O(n log(n)) performance on many data sets that cause other quicksorts to degrade to quadratic performance, and is typically faster than traditional (one-pivot) Quicksort implementations.
According to the Java 7 API doc for objects:
The implementation was adapted from Tim Peters's list sort for Python ( TimSort). It uses techniques from Peter McIlroy's "Optimistic Sorting and Information Theoretic Complexity", in Proceedings of the Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474, January 1993.

Tricky SQL queries

In SQL, what is the difference between count(column) and count(*)?


count(*) counts NULLs and count(column) does not


create table ExampleTable(id1 int, id2 int)
insert ExampleTable values(null,null)
insert ExampleTable values(1,null)
insert ExampleTable values(null,1)
insert ExampleTable values(1,null)
insert ExampleTable values(null,1)
insert ExampleTable values(1,null)
insert ExampleTable values(null,null)

 select count(*),count(id1),count(id2) from ExampleTable
 results 7 3 2
  
It's worth mentioning that if you have a non-nullable column such as ID, then count(ID) will significantly improve performance over count(*)
  
Another minor difference, between using * and a specific column, is that in the column case you can add the keyword DISTINCT, and restrict the count to distinct values:
 Example 
select column_a, count(distinct column_b)
from table
group by column_a
having count(distinct column_b) > 1;

What's the simplest SQL statement that will return the duplicate values for a given column and the count of their occurrences in an Oracle database table?

For example: I have a JOBS table with the column JOB_NUMBER.
How can I find out if I have any duplicate JOB_NUMBERs, and how many times they're duplicated?
Query: 
select column_name, count(column_name)
from table
group by column_name
having count (column_name) > 1;


How can a column with a default value be added to an existing
table
ALTER TABLE {TABLENAME}
 ADD {COLUMNNAME} {TYPE} {NULL|NOT NULL}
CONSTRAINT {CONSTRAINT_NAME}
DEFAULT {DEFAULT_VALUE}[WITH VALUES]

Class loader in Java

ClassLoaders(CL) in Java

Class Loader:

1. Responsible for loading classes
2. is Abstract class
3. given binary name -> CL should attempt to locate or generate data that    constitues a definition for the class
4. Every Class object contains reference to ClassLoader that defined it.
5. Class objects for array classes are not created by ClassLoader but automaticallt by Java runtime.   If the element type is a primitive type, then array class has no class loader.
6. Class.getClassLoader()
7. typically used by security managers to indicate security domains
8. ClassLoader class uses a delegation model to search classes and resources.   Each instance of ClassLoader has an assocaited parent class loader.
9. When requested to find a class or resource, a ClassLoader instance delegate the search for the class or resource to its parent class loader before attempting to find class itself.
10. Virtual machine build in class loader called "BootStrap class loader" does not ahve parent but may serve as parent of classloader instance.
11. Normally, JVM loades classes from local file system  in paltform dependent manner.
12. Some classes may not originate from a file, they may originate from other sources. The method defineClass converts bytes into an instance of class "Class". Instance of newly defined is class can be created using Class.newInstance.
13. Method and constructor of objects created by class loader may reference to other class.    JVM uses loadClass method of the classs loader that originally created the class.      

Layered Architecture V/S Tiered Architecture

Logical layers are merely a way of organizing your code.
Typical layers include Presentation, Business and Data – the same as the traditional 3-tier model. But when we’re talking about layers, we’re only talking about logical organization of code.
In no way is it implied that these layers might run on different computers or in different processes on a single computer or even in a single process on a single computer. All we are doing is discussing a way of organizing a code into a set of layers defined by specific function.

Physical tiers however, are only about where the code runs. Specifically, tiers are places where layers are deployed and where layers run. In other words, tiers are the physical deployment of layers.

A "Tier" is a unit of deployment, while a "Layer" is a logical separation of responsibility within code. You may say you have a "3-tier" system, but be running it on one laptop. You may say your have a "3-layer" system, but have only ASP.NET pages that talk to a database. There's power in precision.

Saturday 11 April 2015

Inversion of control

Class ArrangeDuties
{
    Employee e;

    ArrangeDuties(int type)
    {
if(type==1)
           e = TechEmp();
else if(type==2)
           e = HREmp();
    }

    public void allocateDuties()
    {
e.allocateDuties();
    }
}

class TechEmp()
{
    public void allocateDuties()
    {
    }  
}

class HREmp()
{
    allocateDuties()
    {
    }  
}


Class User
{
    public static void main(String args[])
    {
ArrangeDuties a = new ArrangeDuties(1);
a.allocateDuties();
    }

}


Right now ArrangeDuties is controlling how to create object and which object to create.
This ArrangeDuties  and Emplyee classes are tightly coupled.

Take away tight coupling by inverting who we create the Employee objects



Class ArrangeDuties
{
    Employee e;

    ArrangeDuties(Employee e)
    {
this.e = e;
    }

    allocateDuties()
    {
e.allocateDuties();
    }
}

class TechEmp()
{
     public void  allocateDuties()
    {
    }  
}

class HREmp()
{
    public void allocateDuties()
    {
    }  
}


Class User
{
    public static void main(String args[])
    {
Employee e = new TechEmp();
ArrangeDuties a = new ArrangeDuties(e);
a.allocateDuties();
    }
}


Now control is inverted. User class is deciding which object to make and ArrangeDuties is independent of that.

Friday 9 January 2015

Synchronization is lot more than Mutual Exclusion

In multiprocessor systems, processors generally have one or more layers of memory cache. Cache speeds up speed of accessing the data as data is closer to memory.
It also reduces traffic on the shared memory bus as many memory operations can be done using cache only.
Using cache effects the concurrent execution of program.
At the processor level, a memory model defines necessary and sufficient conditions for knowing that writes to memory by other processors are visible to the current processor, and writes by the current processor are visible to other processors.
Types of memory model:
  1.     Strong  memory model: For a given memory location, all processors see the same value
  2.     Weaker memory model: special instructions, called memory barriers, are required to flush or invalidate the local processor cache in order to see writes made by other processors or make writes by this processor visible to others.
These memory barriers are usually performed when lock and unlock actions are taken; they are invisible to programmers in a high level language.

Recent trends in processor design have encouraged weaker memory models, because the relaxations they make for cache consistency allow for greater scalability across multiple processors and larger amounts of memory.

The issue of when a write becomes visible to another thread is compounded by the compiler's reordering of code.
A simple example of this can be seen in the following code:
Class Reordering {
  int x = 0, y = 0;
  public void writer() {
    x = 1;
    y = 2;
  }

  public void reader() {
    int r1 = y;
    int r2 = x;
  }
}

Let's say that this code is executed in two threads concurrently, and the read of y sees the value 2. Because this write came after the write to x, the programmer might assume that the read of x must see the value 1. However, the writes may have been reordered. If this takes place, then the write to y could happen, the reads of both variables could follow, and then the write to x could take place. The result would be that r1 has the value 2, but r2 has the value 0.

The Java Memory Model describes what behaviors are legal in multithreaded code, and how threads may interact through memory. It describes the relationship between variables in a program and the low-level details of storing and retrieving them to and from memory or registers in a real computer system. It does this in a way that can be implemented correctly using a wide variety of hardware and a wide variety of compiler optimizations.

There are a number of cases in which accesses to program variables (object instance fields, class static fields, and array elements) may appear to execute in a different order than was specified by the program. The compiler is free to take liberties with the ordering of instructions in the name of optimization. Processors may execute instructions out of order under certain circumstances. Data may be moved between registers, processor caches, and main memory in different order than specified by the program.
For example, if a thread writes to field a and then to field b, and the value of b does not depend on the value of a, then the compiler is free to reorder these operations, and the cache is free to flush b to main memory before a. There are a number of potential sources of reordering, such as the compiler, the JIT, and the cache.

Incorrectly synchronized code can mean different things to different people. When we talk about incorrectly synchronized code in the context of the Java Memory Model, we mean any code where
1.     there is a write of a variable by one thread,
2.     there is a read of the same variable by another thread and
3.     the write and read are not ordered by synchronization
When these rules are violated, we say we have a data race on that variable. A program with a data race is an incorrectly synchronized program.

What does synchronization do?

Synchronization has several aspects.
1.     Mutual exclusion: only one thread can hold a monitor at once, so synchronizing on a monitor means that once one thread enters a synchronized block protected by a monitor, no other thread can enter a block protected by that monitor until the first thread exits the synchronized block.
2.      Synchronization ensures that memory writes by a thread before or during a synchronized block are made visible in a predictable manner to other threads which synchronize on the same monitor.

Exit Synchronized block --> Release Moniter -> Flush cache to main memory.
                                                          Write by this thread is visible to all other threads


Before enter synchronnized block  -> Acquire monitor --> Invalidate local processor cache so memory will be reloaded from main memory. (Now this thread can see changes by other thread)





The new memory model semantics create a partial ordering on memory operations.
When one action happens before another, the first is guaranteed to be ordered before and visible to the second. The rules of this ordering are as follows:
  • Each action in a thread happens before every action in that thread that comes later in the program's order.
  • An unlock on a monitor happens before every subsequent lock on that same monitor.
  • A write to a volatile field happens before every subsequent read of that same volatile.
  • A call to start() on a thread happens before any actions in the started thread.
  • All actions in a thread happen before any other thread successfully returns from a join() on that thread.

synchronized (new Object()) {}
Will not work because compiler remove this block.
Reason: compiler knows that no other thread can synchronized on the same object/monitor.
Writing to a volatile field has the same memory effect as a monitor release, and reading from a volatile field has the same memory effect as a monitor acquire. 



Saturday 3 January 2015

How to check whether String class is mutable or immutable programmatically in java

We can use hashCode() to check whether two string object have same hash code.
In this example, we have created a new String class object with value "Mutable".
Later, we have used  replace() to change the string str and resulting value is updated in str reference variable.
Now, if previous hashcode for str is equal to new hash code for str, then String class will be mutable otherwise it will be immutable.

public class StringMutable
{
    public static void main(String[] args)
{
String str = new String("Mutable");
int previousStrHashCode = str.hashCode();
str = str.replace('u', 'a');  // A new string object is created
int newHashCode = str.hashCode(); //  a new String object will have a new hashcode
System.out.println("prev "+previousStrHashCode + "   new : "+newHashCode );
if(previousStrHashCode == newHashCode)
{
System.out.println("Mutable");
}
else
{
System.out.println("Immutable");
}
}
}

Output:
prev -1216934138   new : -1789517158
Immutable

Here, hash code is different for string object before modification and after modification. Hence, a new object was created created instead of updating the previous String object.
This proves that String class is immutable. 



Equals and hascode method in java

Example of equals and hashcode method in java


This document explains :Why equals and hashcode methods are require and how to implement them.

If you want to use a class as a key in MAP/SET, provide proper hashcode() and equals() method implementation for that class.

Reason: If you don't provide implementation for these  methods class will inherit these from superclass implementation of these methods(generally Object class) and map/set may behave incorrectly.
Lets’ take an example of employee class

Employee.java

public class Employee
{
       String employeeId;
       enum EmpType{FULLTIMEPARTTIMEINTERN};
       String type;
       String joiningYear;
      
       public Employee(String empId, String type, String jy)
       {
              this.employeeId = empId;
              this.type = type;
              this.joiningYear = jy;
       }
      
       public void show()
       {
              System.out.println("Employee ID : "+employeeId + "  Employee Type "type+" joining year  "+joiningYear);
       }
      
       @Override
       public String toString()
       {
              return employeeId + "  "+type + "   "+joiningYear;
       }
}


 MainClass1.java

import java.util.HashMap;
import java.util.Map;
public class MainClass1 {
                public static void main(String[] args)
                {
                                Map<Employee,Float> map = new HashMap<Employee, Float>();
                                Employee e1 = new Employee("Mayank Sharma", EmpType.FULLTIME.toString(), "2013");
                                Employee e2 = new Employee("Nikhil Kumar", EmpType.INTERN.toString(), "2014");
                                Employee e3 = new Employee("Love Sharma", EmpType.PARTTIME.toString(), "2013");               
                                Employee e4 = new Employee("Kapil Kumar", EmpType.FULLTIME.toString(), "2002");                                   Employee e5 = new Employee("Neeraj Panwar", EmpType.INTERN.toString(), "2014");
                                Employee e6 = new Employee("David Buther", EmpType.PARTTIME.toString(), "2011");
                                Employee e7 = new Employee("David Buther", EmpType.PARTTIME.toString(), "2011");
                                map.put(e1, (float) 500000);
                                map.put(e2, (float) 50000);
                                map.put(e3, (float) 400000);
                                map.put(e4, (float) 1500000);
                                map.put(e5, (float) 50000);
                                map.put(e6, (float) 80000);
                                map.put(e7, (float) 90000);
                                for(Employee employee: map.keySet())
                                {
                                                employee.show();
                                                System.out.println(map.get(employee));
                                }
                }
}

Output:

Employee ID : David Buther  Employee Type PARTTIME joining year  2011
90000.0
Employee ID : Neeraj Panwar  Employee Type INTERN joining year  2014
50000.0
Employee ID : Kapil Kumar  Employee Type FULLTIME joining year  2002
1500000.0
Employee ID : Nikhil Kumar  Employee Type INTERN joining year  2014
50000.0
Employee ID : Love Sharma  Employee Type PARTTIME joining year  2013
400000.0
Employee ID : David Buther  Employee Type PARTTIME joining year  2011
80000.0
Employee ID : Mayank Sharma  Employee Type FULLTIME joining year  2013
500000.0

Here object “e7” and “e6” have same hash code, but e7 should have override the e6 but map contains both e6 and e7 which shows that but e6 and e7 should be same since they have same value set.

Now override equals method in Employee class:
       @Override
       public boolean equals(Object o)
       {
              if(o == this)
                     return true;
              if(!(o instanceof Employee1))
              {
                     return false;
              }
              Employee1 employee1 = (Employee1)o;
              return this.employeeId.equals(employee1.employeeId)
                     && this.type.equals(employee1.type)
                     && this.joiningYear.equals(employee1.joiningYear);
       }

Add try to run the same program.
You will get same output. It is because hashcode() is not overridden in employee class and we are using the same previous hashcode which says that two different object will have same hashcode.
Now lets, override the hashcode() in Employee class :
       @Override
       public int hashCode()
       {
              return 42;
       }

All the objects have same hash code, so they all will added in a particular entry of linked list and hence form a linked list.
Now run the program.
Wow. You will get the desired output.
Output:

Employee ID : Mayank Sharma  Employee Type FULLTIME joining year  2013
500000.0
Employee ID : Nikhil Kumar  Employee Type INTERN joining year  2014
50000.0
Employee ID : Love Sharma  Employee Type PARTTIME joining year  2013
400000.0
Employee ID : Kapil Kumar  Employee Type FULLTIME joining year  2002
1500000.0
Employee ID : Neeraj Panwar  Employee Type INTERN joining year  2014
50000.0
Employee ID : David Buther  Employee Type PARTTIME joining year  2011
90000.0
There is no duplicate entry for employee with employee name : David Buther
You can provide following implementation of hashcode for better performance:
       @Override
       public int hashCode()
       {
              int result  =17;
              result = 31* result + employeeId.hashCode();
              result = 31*result +  type.hashCode() ;
              result = result* 31 +joiningYear.hashCode();
              return result;
       }

This method avoid collision and final map will not looks like a linked list as in the previous case.
Where the equals and hashcode methods are use?
Let’s take the HashMap put method implementation:
    public V put(K key, V value) {
        if (table == EMPTY_TABLE) {
            inflateTable(threshold);
        }
        if (key == null)
            return putForNullKey(value);
        int hash = hash(key);
        int i = indexFor(hash, table.length);
        for (Entry<K,V> e = table[i]; e != null; e = e.next) {
            Object k;
            if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
                V oldValue = e.value;
                e.value = value;
                e.recordAccess(this);
                return oldValue;
            }
        }

        modCount++;
        addEntry(hash, key, value, i);
        return null;
    }

And here is the implementation of hash() :

    final int hash(Object k) {
        int h = hashSeed;
        if (0 != h && k instanceof String) {
            return sun.misc.Hashing.stringHash32((String) k);
        }

        h ^= k.hashCode();

        // This function ensures that hashCodes that differ only by
        // constant multiples at each bit position have a bounded
        // number of collisions (approximately 8 at default load factor).
        h ^= (h >>> 20) ^ (h >>> 12);
        return h ^ (h >>> 7) ^ (h >>> 4);
    }





Singleton design pattern

Singleton design pattern


Ensure a class only has one instance, and provide a global point of access to it.
The Singleton's purpose is to control object creation, limiting the number of objects to one only. 
Sometimes it's important to have only one instance for a class.


A class that implements the singleton design pattern must prevent multiple instantiations. Relevant techniques include:
1. Make constructor private
2. Provide a public static method to accessing/sharing the singleton object. 

Real time Application of Singeton class:
1. Logger Classes
2. Configuration Classes
3. Factories implemented as Singletons

First Solution:
Use a private constructor
Use static method to get Single object
  
class SingleTon
{
    private static final single;

    private SingleTon() {} 

    public static SingleTon SingleTon()
    {
       if(single==null)
       {
             single = new SingleTon();
       }
       return single;
   }
}

Cons: fail in multithreaded environment



Second Solution: Multi threaded version: use synchronized method

class SingleTon
{
    private static final single; 
    private SingleTon() {}
    public static SingleTon synchronized SingleTon()
    {
         if(single==null)
         {
              single = new SingleTon();
         }
         return single;
    }
}


Third solution: synchronized block with double checking

class SingleTon
{
    private static final  volatile single; 
    private SingleTon() {}
    public static SingleTon SingleTon()
    {
         if(single==null)
         {
               synchronized(single)
               {
                     if(single==null)
                     {
                            single = new SingleTon();
                     }
               }
          }
          return single;
    }
}

Fourth Solution: Solution for serialization:  use readResolve() 

class SingleTon implements Serializable
{
    private static final single; 
    private SingleTon() {}
    public static SingleTon syncronized SingleTon()
    {
         if(single==null)
         {
              single = new SingleTon();
         }
         return single;
    }

   private Object readResolve()
   {
         // Return the one true object and let the garbage collector take care of the new object
         return INSTANCE;
   }
}


Fifth solution: Avoid cloning: throw clone not supported exception

Add following method in Singleton class:

public Object clone() throws CloneNotSupportedException
{
      throw new CloneNotSupportedException("");
}



Solution six: Singleton class loaded by different class loaders

If you want to create true singleton, you should avoid using custom classloaders - all singletons should be loaded by common parent classloader.
If you want to make sure the same classloader loads your singletons, you must specify the 
classloader yourself

private static Class getClass(String classname) throws ClassNotFoundException {
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    if(classLoader == null)
        classLoader = Singleton.class.getClassLoader();
    return (classLoader.loadClass(classname));

}


Please note:

Cases where singleton may break:

1. Multiple Singletons in Two or More Virtual Machines
When copies of the Singleton class run in multiple VMs, an instance is created for each machine.

2. Multiple Singletons Simultaneously Loaded by Different Class Loaders
When two class loaders load a class, you actually have two copies of the class, and each one can have its own Singleton instance.

3. Singleton Classes Destroyed by Garbage Collection, then Reloaded
When a Singleton class is garbage-collected and then reloaded, a new Singleton instance is created. Any class can be garbage-collected when no other object holds reference to the class or its instances. If no object holds a reference to the Singleton object, then the Singleton class may disappear, later to be reloaded when the Singleton is again needed. In that case, a new Singleton object will be created. Any static or instance fields saved for the object will be lost and reinitialized.

ReadResolve method explanation: 

readResolve is used for replacing the object read from the stream. The only use I've ever seen for this is enforcing singletons; when an object is read, replace it with the singleton instance. This ensures that nobody can create another instance by serializing and deserializing the singleton.

ReadResolve is called after readObject has returned (conversely writeReplace is called before writeObject and probably on a different object). The object the method returns replaces this object returned to the user of ObjectInputStream.readObject and any further back references to the object in the stream. It is mostly used for serial proxies