Safe Singleton for your multi-core servers

Purpose of a Singleton is to have one copy of an object in JVM. But writing below code without volatile in a multi core environment may result in multiple copies of Singleton if any failover occurs.
In a multi-core server multiple Threads of your code may try to create a Singleton object. If singleton variable ( _instance ) is properly not visible to all Threads in all running conditions then Singleton will appear with multiple copies ( which is never expected ).To avoid such cases _instance variable should be declared volatile to make sure if a Thread is creating Singleton instance and just after creation it lost the CPU, all other threads should be communicated that _instance is not null now.

public class Singleton{

private static volatile Singleton _instance;
public static Singleton getInstance(){
if(_instance == null){
if(_instance == null)
_instance = new Singleton();
return _instance;

A Practical scenario for using volatile in java

working example of “volatile” for Java7+ is a highly used searching keyword
for java guys. After lots of search people hardly get any working and effective
code on Volatile. So I decided to put one sample code here on Volatile.

A short note on volatile #
Volatile keyword in Java is used as an indicator to Java compiler and Thread that do not cache value of this variable and always read it from main memory. Java guarantees that value of volatile variable will always be read from main memory and not from Thread’s local cache.So if you want to share any variable in which read and write operation is atomic by implementation e.g. read and write in int or boolean variable you can declare them as volatile variable. This guarantees visibility of changes made by one thread to another thread also.Java volatile keyword cannot be used with method or class
and it can only be used with variable. Java volatile keyword also guarantees visibility and ordering , after Java 5 write to any volatile variable happens before any read into volatile variable. By the way use of volatile keyword also prevents compiler or JVM from reordering of code or moving away them from synchronization barrier.

Below is one example based on volatile, in which different Threads will try to search a number is different range if any thread finds the number search operation will be stopped by all Threads.

 public class SerachUsingVolatile {

	// search range starts with
	static int START = 0;
	static int END = 10000;

	public static void main(String[] args) {

		NumberFinder t = new NumberFinder(28000);

		Thread t1 = new Thread(t, "T1");
		Thread t2 = new Thread(t, "T2");
		Thread t3 = new Thread(t, "T3");


		try {
		} catch (InterruptedException e) {




class NumberFinder implements Runnable {
	//volatile boolean keepRunning = true;
        boolean keepRunning = true;
	int numToFind;

	NumberFinder(int nf) {
		this.numToFind = nf;

	public void run() {

		int start = SerachUsingVolatile.START;
		int end = SerachUsingVolatile.END;

		// move range of search for each Thread
		SerachUsingVolatile.START = SerachUsingVolatile.START + 10000;
		SerachUsingVolatile.END = SerachUsingVolatile.END + 10000;

		while (keepRunning) {

			for (int i = start; i <= end; i++) {
				if (i == numToFind) {
					keepRunning = false;
					System.out.println("Target found by Thread: " + Thread.currentThread().getName()
							+ " rest all Threads should stop searching !!! ");
		System.out.println(" Search stopped for Thread :" + Thread.currentThread().getName());

Output with volatile :

Search stopped for Thread :T1
Target found by Thread: T2 rest all Threads should stop searching !!!
Search stopped for Thread :T3
Search stopped for Thread :T2

Output without volatile :

Target found by Thread: T2 rest all Threads should stop searching !!!
Search stopped for Thread :T2


That’s all Guys !!!! I am open for your questions n comment 🙂