Shankh is London based consultancy started on 2011 to provide consultancy services primarily in Java. With several successful Java projects under our belt, we started exploring mobile based services towards end of 2012 which led to our first android/ios/windows-phone app which is staged for release in 2013

Some Random quotes


Innovation distinguishes between a leader and a follower. -Steve Jobs

It is not that i am smarter than others, i just persist with problems longer

Java Concurrency and volatile

I came across an interesting behaviour of threads in James Roper’s Blog (http://jazzy.id.au/pebble/2009/04/24/java_concurrency_and_volatile.html).  Tested it on Vista 64 bit machine and without a volatile keyboard, in my machine also the code goes into an infinite loop.


public class ThreadExperiement implements Runnable {


	private volatile String str;
    void setStr(String str)
    {
        this.str = str;
    }
    public void run()
    {
        while (str == null){}
        System.out.println(str);
        
    }
    public static void main(String[] args) throws Exception
    {
    	ThreadExperiement threadExp = new ThreadExperiement();
        new Thread(threadExp).start();
        Thread.sleep(1000);
        threadExp.setStr("Hello world!!");
    }

}

Put a print statement inside the while loop and it starts working. Strange , isn’t it?

Is it because of byte code optimisation which detects that there is nothing to do inside the loop ?

Share

Leave a Reply