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.
1 week ago