Tuesday, 30 June 2015

Create immutable class


/**
 * Create Immutable Class
 * 
 * @author:http://javainterviewprograms.blogspot.in/
 * 
 */

public final class ImmutableClass {

 private final String name;

 public ImmutableClass(String name)
 {
  this.name=name;
 }
 public String getName()
 {
  return name;
 }
}


To make class immutable :
1.instance variable final
2.class final
3.No setter method


Thread Dead lock solution ?

package com.JavaInterview.Programs;

/** * 
 * @author:http://javainterviewprograms.blogspot.in/
 * 
 */
public class DeadlockSolution {

 public static Object lock1 = new Object();
 public static Object lock2 = new Object();

 public static void main(String args[]) {
  Thread1 t1 = new Thread1();
  Thread2 t2 = new Thread2();

  t1.start();
  t2.start();
 }

 private static class Thread1 extends Thread {

  public void run() {
   synchronized (lock1) {
    System.out.println("Thread 1 :holding the lock1 ...");
    try {
     Thread.sleep(10);
    } catch (InterruptedException e) {
    }
    System.out.println("Thread 1 :waiting for lock 2....");
    synchronized (lock2) {
     System.out.println("Thread 1: holding lock1 and 2...");
    }
   }
  }

 }

 private static class Thread2 extends Thread {
  public void run() {
   synchronized (lock1) // to create deadlock use lock2 here
   /*
    * By interchanging locks position we have avoided deadlock
    * here(From creating deadlock example)
    */
   {
    System.out.println("Thread 2 :holding the lock2");
    try {
     Thread.sleep(20);
    } catch (InterruptedException e) {
    }
    System.out.println("Thread 2 : waiting for lock2 ...");
    synchronized (lock2)// to create deadlock use lock1 here
    {
     System.out.println("Thread 2 : holding both lock1 and 2..");
    }
   }
  }
 }
}


Output:
Thread 1 :holding the lock1 ...
Thread 1 :waiting for lock 2....
Thread 1: holding lock1 and 2...
Thread 2 :holding the lock2
Thread 2 : waiting for lock2 ...
Thread 2 : holding both lock1 and 2..


Create Thread dead lock ?

package com.JavaInterview.Programs;
/**
 * Find Out Factorial of a number
 * 
 * @author:http://javainterviewprograms.blogspot.in/
 * 
 */
public class ThreadDeadlock {

 public static Object lock1 = new Object();
 public static Object lock2 = new Object();

 public static void main(String args[]) {
  Thread1 t1 = new Thread1();
  Thread2 t2 = new Thread2();

  t1.start();
  t2.start();
 }

 private static class Thread1 extends Thread {

  public void run() {
   synchronized (lock1) {
    System.out.println("Thread 1 :holding the lock1 ...");
    try {
     Thread.sleep(10);
    } catch (InterruptedException e) {
    }
    System.out.println("Thread 1 :waiting for lock 2....");
    synchronized (lock2) {
     System.out.println("Thread 1: holding lock1 and 2...");
    }
   }
  }

 }

 private static class Thread2 extends Thread {
  public void run() {
   synchronized (lock2) {
    System.out.println("Thread 2 :holding the lock2");
    try {
     Thread.sleep(20);
    } catch (InterruptedException e) {
    }
    System.out.println("Thread 2 : waiting for lock1 ...");
    synchronized (lock1) {
     System.out.println("Thread 2 : holding both lock1 and 2..");
    }
   }
  }
 }
}


Output :
Thread 2 :holding the lock2
Thread 1 :holding the lock1 ...
Thread 1 :waiting for lock 2....
Thread 2 : waiting for lock1.
...


Monday, 29 June 2015

Basic HashSet Operations -Adding elements ,adding duplicates ,removal?

package com.JavaInterview.Programs;
import java.util.HashMap;
import java.util.HashSet;

/*
 * 
 */

public class HashsetTest {

 
 public static void main (String args[])
 {
  HashSet<String> h= new HashSet<String>();
 
 
  h.add("abc");
  h.add("bcd");
 
  System.out.println("hashset sample"+h);
 
  //adding duplicates object
  h.add("abc");
  System.out.println("After adding duplicates key"+h);
 
  //HashSet  size
  System.out.println("HashSet size "+h.size());
 
  //remove elements
  h.remove("abc");
  System.out.println("After removal "+h);
 }
}

Output:
hashset sample[abc, bcd]
After adding duplicates key[abc, bcd]
HashSet size 2

After removal [bcd]



Write HashMap Programme to display basic functionaliy -After adding duplicate keys ,remove etc?

package com.JavaInterview.Programs;

import java.util.*;

/**
 * Find Out Factorial of a number
 * 
 * @author:http://javainterviewprograms.blogspot.in/
 * 
 */
public class HashmapTest {

 public static void main (String args[])
 {
  HashMap String,Integer> h= new HashMap String,Integer>();
 
  /*HashMap String,int> h= new HashMap String,int>();compiler error |You can't use
  primitive types as generic arguments in Java*/
  h.put("abc",12);
  h.put("bcd", 13);
 
  System.out.println("hashmap sample"+h);
 
  //adding duplicates keys
  h.put("abc",14);
  System.out.println("After adding duplicates key"+h);
 
  //adding duplicates value
  h.put("cdf",13);
  System.out.println("After adding duplicates value"+h);
 
  //adding duplicates keys again
  h.put("abc",15);
  System.out.println("Again after adding duplicates key"+h);
 
  //fetch by key
  System.out.println("Fetch map by its value"+h.get("abc"));
  System.out.println("Fetch map by its value"+h.get("bcd"));
 
  //HashMap Empty
  System.out.println("Is HashMap empty "+h.isEmpty());
 
  //remove elements
  h.remove("abc");
  System.out.println("After removal "+h);
 }
}


Output :
hashmap sample{abc=12, bcd=13}
After adding duplicates key{abc=14, bcd=13}
After adding duplicates value{abc=14, bcd=13, cdf=13}
Again after adding duplicates key{abc=15, bcd=13, cdf=13}
Fetch map by its value15
Fetch map by its value13
Is HashMap empty false
After removal {bcd=13, cdf=13}


Write a Singleton Example -Thread Safe ?

/*Singelton Example using Double checked (Thread Safe)*/
public class MySingleton {
 private static volatile MySingleton obj;

 //private constructor

 private MySingleton()
 {
 
 }

 //Instance method
    public static MySingleton getInstance()
    {
     if(obj==null)  //single checked
     {
      synchronized(MySingleton.class)
      {
       if(obj==null) //double checked
       {
          obj=new MySingleton();
          }
      }
     }
     return obj;
    }
    
    public void print()
    {
     System.out.println("this is my Singleton Example");
    }
    
    public void main(String args[])
    {
     MySingleton m=MySingleton.getInstance();
     m.print();
    }
}


Difference between final, finally and finalize()?

final :Final is a keyword. Final is used to apply restrictions on class, method and variable. Final class can't be inherited, final ...