Wednesday, March 11, 2015

when to use ThreadLocal class in java

MultiThreaded Programming is always special in java. It has lot to do. In such cases we will face problems like sharing of variables (which has static specifier in my case). Synchronization is not exactly suitable then we can use thread local.
Java ThreadLocal is used to create thread-local variables. We know that all threads of an Object share it’s variables, so if the variable is not thread safe, we can use synchronization but if we want to avoid synchronization, we can use ThreadLocal variables.
Every thread has it’s own ThreadLocal variable and they can use it’s get() and set() methods to get the default value or change it’s value local to Thread. ThreadLocal instances are typically private static fields in classes that wish to associate state with a thread.

ThreadLocal class is extend in Java 8 with a new method withInitial() that takes Supplier functional interface as argument. So we can use lambda expressions to easily create the ThreadLocal instance.
Here is a small example showing use of ThreadLocal and proving that every thread has it’s own copy of ThreadLocal variable.
public class XmlParserProcessor {
//Variable for each thread stack when more number of threads running simultaneously .
    public static final ThreadLocal<Integer> count = new ThreadLocal<Integer>(){
        @Override
        protected Integer initialValue()
        {
            return 0;
        }
    }; 

 public static void set(int skippedCount) {    
        skippedListCount.set(skippedCount);    
    }
    public static void unset() {    
        skippedListCount.remove();    
    }
    public static int get() {        
        return skippedListCount.get();    
    }

}

Tuesday, December 23, 2014

Java Memory Areas

Java Memory Areas



Java program has one or more threads. Each thread has its own stack.  All threads share same heap 




JVM Machine Architecture


Java Implementation :




  • Compiler and Virtual Machine
  • Compiler produces bytecode
  • Virtual machine loads classes on demand, verifies bytecode properties, interprets bytecode
  • Why this design?  Bytecode interpreter/compilers used before Pascal “pcode”; Smalltalk compilers use bytecode Minimize machine-dependent part of implementation Do optimization on bytecode when possible Keep bytecode interpreter simple For Java, this gives portability Transmit bytecode across network.


Monday, December 22, 2014

Interview Questions on java

What is the Difference between JDK and JRE?

Ans :  The “JDK” is the Java Development Kit i.e., the JDK is bundle of software that you can use to develop Java based software.
The “JRE” is the Java Runtime Environment i.e., the JRE is an implementation of the Java Virtual Machine which actually executes Java programs.

Typically, each JDK contains one (or more) JRE’s along with the various development tools like the Java source compilers, bundling and deployment tools, debuggers, development libraries, etc.


Describe what happens when an object is created in Java?
Several things happen in a particular order to ensure the object is constructed properly: 1. Memory is allocated from heap to hold all instance variables and implementation-specific data of the object and its super classes. Implementation-specific data includes pointers to class and method data. 2. The instance variables of the objects are initialized to their default values. 3. The constructor for the most derived class is invoked. The first thing a constructor does is call the constructor for its uppercase. This process continues until the constructor for java.lang.Object is called, as java.lang.Object is the base class for all objects in java. 4. Before the body of the constructor is executed, all instance variable initializers and initialization blocks are executed. Then the body of the constructor is executed. Thus, the constructor for the base class completes first and constructor for the most derived class completes last.

In Java, you can create a String object as below : String str = "abc"; & String str = new String("abc"); Why cant a button object be created as : Button bt = "abc"? Why is it compulsory to create a button object as: Button bt = new Button("abc"); Why this is not compulsory in String’s case?
Button bt1= "abc"; It is because "abc" is a literal string (something slightly different than a String object, by-the-way) and bt1 is a Button object. That simple. The only object in Java that can be assigned a literal String is java.lang.String. Important to not that you are NOT calling a java.lang.String constuctor when you type String s = "abc"; For example String x = "abc"; String y = "abc"; refer to the same object. While String x1 = new String("abc");
String x2 = new String("abc"); refer to two different objects.


What is the advantage of OOP?
You will get varying answers to this question depending on whom you ask. Major advantages of OOP are:
1. Simplicity: software objects model real world objects, so the complexity is reduced and the program structure is very clear;
2. Modularity: each object forms a separate entity whose internal workings are decoupled from other parts of the system;
3. Modifiability: it is easy to make minor changes in the data representation or the procedures in an OO program. Changes inside a class do not affect any other part of a program, since the only public interface that the external world has to a class is through the use of methods;
4. Extensibility: adding new features or responding to changing operating environments can be solved by introducing a few new objects and modifying some existing ones;
5. Maintainability: objects can be maintained separately, making locating and fixing problems easier;
6. Re-usability: objects can be reused in different programs.