Thursday, September 18, 2008

Bernard, Shenton waycoffeebean shop, java volatile

Some time back I suppose to meet “java troopers” Bernard Ng in the early morning eight O’ clock at Shenton way coffeebean shop, I am a night owl, early wake up is one of the tougher job for me.

I can say that the Java trooper Bernard is one of the geniuses in Java.

In that discussion, Bernard wants to test my coding ability to that before he gives green signal for a company who are interested to recruit me, so he asked me to write java code using threads. The question is very simple; Write a program to invoke one java method after completes all the threads finish it’s execution.

It’s a straight forward question right? declare a count variable common for these threads, in the last line of the the thread execution, just increase the count of a common variable in a synchronize block, there could be a 4th thread will check the value of that variable, if it is not 3 then this thread will go to sleep for some time and again will check the value of that variable.

Bernard Ng has great knowledge in java and programming languages, I have to impress him to get the job, so I have written this program in bit different way, I plan to use “volatile” even though the use of “volatile” is not the 100% perfect for this problem. “volatile” is not frequently used by programmers, so many of us ignore about volatile, so I just want to use “volatile” to impress him.

We assume something and keep in our mind for years and years without knowing the real fact, the same way I assume that “volatile” is something opposite to “transient” and it was in my mind for many years. I never try to read or learn about “volatile”

Some time back when I was searching job, I refresh my java knowledge again, first few days I skip reading about “volatile” but at last I read about volatile…. A big shock, reasons are

1. “volatile” is not opposite to “transient” or something like “transient” in java
2. “volatile” is related to thread and not related to serialization…

I was really ashamed on me then I started looking for “volatile”

In a mutli-thread environment, a variable can be accessed by many threads and asynchronously change the values of that variable, so when a thread access that variable, it does not guarantee that the value is the latest one, once a variable is marked as “volatile”, who ever access this variable, gets the latest value of the variable, so it’s some way equals to synchronized without overhead of the synchronized keyword.

Marking local variable is “volatile” is not appropriate, since other threads cannot see local variables, there is never any need to mark local variables volatile.

I declare a variable as “volatile” and I haven’t use synchronize block for thread safety, except this, I use the same approach mentioned above, but in this approach also there is a very less possibility for the code might not work.

Increasing the volatile variable count++ might not work as expected, even though count++ looks single operation, it comprise three operations.

Read the value of the variable, increase one for the value of the variable, assign back to the increased value to the variable, if a thread modify the value of that variable After read the value of the variable and before assign the increased value back to the variable, then it might not work as expected, even though this unlikely happened, still there are chances, that’s why I said this is not the 100% perfect solution for Bernard’s question.


Anonymous said...

Dear Author !
I congratulate, what words..., a remarkable idea

Anonymous said...

I want to quote your post in my blog. It can?
And you et an account on Twitter?

javaPuru said...

//I want to quote your post in my blog. It can?
And you et an account on Twitter?//
Hi, yes you can quote, no issues... I too have twitter but donot use it regularly

Anonymous said...

What a great web log. I spend hours on the net reading blogs, about tons of various subjects. I have to first of all give praise to whoever created your theme and second of all to you for writing what i can only describe as an fabulous article. I honestly believe there is a skill to writing articles that only very few posses and honestly you got it. The combining of demonstrative and upper-class content is by all odds super rare with the astronomic amount of blogs on the cyberspace.

Abel Ahar said...

Here we present to introduce to all visitors in this great blog. and thank you for allowing me .

cara menggugurkan kandungan
obat aborsi
tanda tanda kehamilan

boss patel said...

JAC Result
UP Board 12th Result 2017
HBSE 12th Result 2017
GSEB HSC Result 2017

Martin sam said...

Bihar board Inter Result 2017
JAC Inter Result 2017
UP Board Inter Result 2017
JAC Matric Result
JAC Inter Result

gondes ribut said...

This is a very good article material and it is very useful for us all. thank you . cara menggugurkan kandungan