Friday, March 28, 2008

Passing thought..


It was the umpteenth time when I was taken aback by the pathetic plight of the "poor" while I was held up in a traffic jam. My mind froze upon some thoughts while I saw a couple of ill-clad children rush towards the traffic as the cars decelerated at the signal. Each of them knew exactly what to do - They are experts at putting on expressions and each of their voices was also toned well enough to drive "mercy" out of anyone. I was in an auto rickshaw as a part of that traffic jam and I could clearly make out what their "tears" were made up of. It was really shocking! I wish their parents had channelized this much of thinking into something more constructive. I wondered how these people can harness sympathy out of the passers-by but then it is rightly so I guess. It is a real pity that these children are forced into an act which I feel is worse than crime.

The traffic moved on and I was left with nothing but helplessness and remorse. I guess there are many who feel likewise but it needs immense determination and hard work to eradicate such unhealthy components from our neighborhoods which many (including me of course) don't indulge in. It is because we are too selfishly involved in our daily chores and so we think we do not have the time for these.

P.S I am not being judgmental about which course we adopt, its just a thought. But at the end of it I am genuinely happy to know there are dedicated organizations which claim to work towards improvement of the social conditions and I hope they are successful even if by a small percentage.

Monday, March 24, 2008

How 'volatile' are they?

What are volatile variables?

A variable in Java if declared volatile ensures the visibilty of the variable when shared across different threads. Lets consider a variable, say an int i and this variable is declared volatile. Now if Thread 1 changes the value of i from1 to 10 at time t = 0. At time t=1 the value of i which thread 2 reads is 10 and not 1. This is bacause for volatile variables the values which is read is the shared value, which is the most recent value of the variable.

Are they as powerful as locks?

So if the visibility of a shared variable is guaranteed, can one do away with locking with volatile variables?

Consider the following:

A Runnable Class:

public class VolatileRunnable implements Runnable{

private volatile int i;// This is a shared variable

public void run() {

for (int j = 0; j < 10000; j++) {
i++;
}
}

public int getI() {
return i;
}
}



And a main method which uses the Runnable:

public static void main(String[] args) throws InterruptedException {
VolatileRunnable r = new VolatileRunnable();
Thread t1 = new Thread(r);
Thread t2 = new Thread(r);

t1.start();
t2.start();
t1.join();
t2.join();

System.out.println("r.getI() = " + r.getI());
}


Now since the variable i is shared and declared volatile what would be the expected output? To me, going by the definition of volatile the output would be (number of threads) * 10000 (20000 in this case). On running the program I saw the output of 20000 was not always true. There were times when the output was 19445, 19667 etc.

Why is that even if the variable is declared volatile?

The operation i++ is actually a series of read, use and write operations on the variable i. Each of these individual operations are atomic but the combination of them is not.

A snapshot of time

t1 Thread 1 read (i = 3)
t2 Thread 2 read (i = 3)
t3 Thread 1 used (i + 1 = 4)
t4 Thread 2 used (i + 1 = 4)
t5 Thread 1 wrote (i = 4)
t6 Thread 2 wrote (i = 4)


This is a case of lost update. This is what would have happened when the above example was run. Some of the increments were lost and hence the non deterministic output.

Hence in this case even though the variable was volatile atomicity was not guaranteed and so synchronization is indispensable to ensure the expected outcome.

The moral:

Volatile variables are not enough when the expected behaviour in any piece of code depends on the value of a variable before any write operation.