Thursday, April 6, 2017

The Easy Way to Write toString() Method

We can write a toString() method instead of writing more code especially include with lot of variable, it can be possible like below by using Apache Commons library.

public String toString() {
    return ToStringBuilder.reflectionToString(this);
}

Types of Builds in Maven

Usually in maven we have two types of builds

1) Snapshot builds
2) Release builds

Snapshot builds: SNAPSHOT is the special version that indicate current deployment copy not like a regular version, maven checks the version for every build in the remote repository so the snapshot builds are nothing but maintenance builds.

Release builds: Release means removing the SNAPSHOT at the version for the build, these are the regular build versions.

Tuesday, April 4, 2017

What is Hibernate proxy?

By default Hibernate creates a proxy for each of the class you map in mapping file. This class is created by hibernate using CGLIB. 

Proxies are created dynamically by subclassing your object at runtime. The subclass has all the methods of the parent, and when any of the methods are accessed, the proxy loads up the real object from the DB and calls the method for you. Very nice in simple cases with no object hierarchy. Typecasting and instanceof work perfectly on the proxy in this case since it is a direct subclass.

What is groupId, artifactId and archeType in Maven?


What is groupId in maven ?

groupId identifies a particular project uniquely across all projects, so we should follow an naming convention. A very simple and commonly used way of doing this is to use reverse of your domain, i.e. com.javarewind.maven. 

A good way of maintaining the integrity of groupId is to use the project structure. In case the project is consists of multiple modules than every module should append an identifier to the parent groupId. i.e. com.javarewind.maven, com.javarewind.spring, com.javarewind.struts .. etc. 

What is artifactId in maven ?

artifactId is the name of war file without version, if you are creating it by yourself you are free to took any name of your choice in lower case and without any strange symbol. But if this is a third party jar than we have to take the name of jar as suggested by it’s distribution. 

What is archetype in maven ?

Archetype is a Maven project templating toolkit which tells the maven the type of project we are going to create. Archetype enables the maven to create a template project of user’s choice so that the user can get the project up and running instantly. 

“archetype:generate”  generates a new project from provided archetype or update the actual project if using a partial archetype. Maven provides a number of predefined archtypes, see more details from Maven Documentation. 

Monday, April 3, 2017

Difference between sleep() and wait()?

sleep() is a method which is used to hold the process for few seconds or the time you wanted but in case of wait() method thread goes in waiting state and it won’t come back automatically until we call the notify() or notifyAll().
The major difference is that wait() releases the lock or monitor while sleep() doesn’t releases any lock or monitor while waiting. Wait is used for inter-thread communication while sleep is used to introduce pause on execution, generally.
Thread.sleep() sends the current thread into the “Not Runnable” state for some amount of time. The thread keeps the monitors it has acquired — i.e. if the thread is currently in a synchronized block or method no other thread can enter this block or method. If another thread calls t.interrupt() it will wake up the sleeping thread. Note that sleep is a static method, which means that it always affects the current thread (the one that is executing the sleep method). A common mistake is to call t.sleep() where t is a different thread; even then, it is the current thread that will sleep, not the t thread.
object.wait() sends the current thread into the “Not Runnable” state, like sleep(), but with a twist. Wait is called on an object, not a thread; we call this object the “lock object.” Before lock.wait() is called, the current thread must synchronize on the lock object; wait() then releases this lock, and adds the thread to the “wait list” associated with the lock. Later, another thread can synchronize on the same lock object and call lock.notify(). This wakes up the original, waiting thread. Basically, wait()/notify() is like sleep()/interrupt(), only the active thread does not need a direct pointer to the sleeping thread, but only to the shared lock object.
Let categorize all above points :
Call on:
  •     wait(): Call on an object; current thread must synchronize on the lock object.
  •     sleep(): Call on a Thread; always currently executing thread.
Synchronized:
  •     wait(): when synchronized multiple threads access same Object one by one.
  •     sleep(): when synchronized multiple threads wait for sleep over of sleeping thread.
Hold lock:
  •     wait(): release the lock for other objects to have chance to execute.
  •     sleep(): keep lock for at least t times if timeout specified or somebody interrupt.
Wake-up condition:
  •     wait(): until call notify(), notifyAll() from object
  •     sleep(): until at least time expire or call interrupt().
Usage:
  •     sleep(): for time-synchronization and;
  •     wait(): for multi-thread-synchronization.

What is Deadlock in Java?

deadlock is a situation where minimum two threads are holding lock on some different resource, and both are waiting for other’s resource to complete its task. And, none is able to leave the lock on resource it is holding.

Difference between Lock and Monitor - Java Concurrency

A lock is like a privilege that only one thread can "possess" at any one time.

If a thread wants to lock a particular object or class, it asks the JVM. 
When the thread no longer needs the lock, it returns it to the JVM. If another thread has requested the same lock, the JVM passes the lock to that thread.


The JVM uses locks in conjunction with monitors. A monitor is basically a guardian in that it watches over a sequence of code, making sure only one thread at a time executes the code.

Each monitor is associated with an object reference. When a thread arrives at the first instruction in a block of code that is under the watchful eye of a monitor, the thread must obtain a lock on the referenced object.
In Java language terminology, the coordination of multiple threads that must access shared data is called synchronization.


Two opcodes, MonitorEnter and MonitorExit, are used for synchronization blocks within methods.


When MonitorEnter is encountered by the Java virtual machine, it acquires the lock for the object referred to by objectref on the stack. If the thread already owns the lock for that object, a count is incremented. Each time MonitorExit is executed for the thread on the object, the count is decremented. When the count reaches zero, the monitor is released.