This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes Volatile in Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Volatile in Thread" Watch "Volatile in Thread" New topic
Author

Volatile in Thread

Sharmistha Sarkar
Ranch Hand

Joined: Dec 18, 2012
Posts: 44
Hi All,

I am trying to create an example for volatile. I read that volatile gives visibility of a global variable.

So in my code I am using 2 threads , one is incrementing the global variable value and another one is printing the value.

But this code is not working as expected .




I get result sometimes 5 and sometimes 0. Can anyone tell me what's wrong going on?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42374
    
  64
While you didn't say what you did expect, I'm guessing it was to see other values than 0 or 5.

I don't think anything is wrong. Most likely the fact that "count" is volatile never becomes an issue, because t1 runs for way too short a time slice to ever be interrupted during the loop. So it comes down to which thread is run first - which would explain why you only see 0 or 5.


Ping & DNS - my free Android networking tools app
Sharmistha Sarkar
Ranch Hand

Joined: Dec 18, 2012
Posts: 44
Hi Ulf,
I want to run the t1 thread first and t2 thread after that. And output should be 5 and it should never change to show 0 at anytime.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42374
    
  64
If you don't want concurrent execution of two threads then you should not start two threads. It's hard to advise anything else since this is obviously an example that does nothing useful.
Sharmistha Sarkar
Ranch Hand

Joined: Dec 18, 2012
Posts: 44
Oh alright, fine, I have no problem with concurrent execution of two threads. But I just want my output should be constant . It should be only 5 , no 0. I am trying to make an example to work with "volatile" ....

Steps for the program,
1) I have to create a global variable
2) create two or more threads and each thread is trying to change the global variable value.
3) The global variable should be visible to all threads and all the changes made by the threads will reflect and update correctly to the global variable
4) get the final output.


Could you please tell me if I am wrong in this algorithm or in understanding the "volatile" concept itself?

Regards,
Sharmistha
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
Sharmistha Sarkar wrote:I want to run the t1 thread first and t2 thread after that.

Note that the call to t1.join on line 38 does not stop the t2 thread executing - it stops the main thread executing. The main thread waits for t1 to complete, but t2 carries on executing alongside t1.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42374
    
  64
You won't be able to observe a changing variable with a thread that prints the value of that variable exactly once.

I have no problem with concurrent execution of two threads. But I just want my output should be constant . It should be only 5 , no 0.

Those are contradictory statements. If you have two concurrently executing threads then you need to be prepared for them to execute concurrently. Your code does not have control over which concurrent threads are scheduled to run when; that is up to the JVM. If you want t1 to always finish before t2 starts, then you should simply not start t2 before t1 finishes.
Sharmistha Sarkar
Ranch Hand

Joined: Dec 18, 2012
Posts: 44
I really didn't understand why should I use volatile.

It's is about visibility of a global variable which I know. But when should I use this volatile keyword ? At what circumstances ?

If the code which I created; is not correct then can anyone create some other program or modify my code and explain?

Regards,
Sharmistha
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42374
    
  64
It's not that the code is not correct, it's just that both threads are so short-lived that you won't see one of them intercepted by the other. And t2 only attempts to print a single time, so it's not useful to observe a change in value to begin with.
Sharmistha Sarkar
Ranch Hand

Joined: Dec 18, 2012
Posts: 44
i don't know now how can I make the threads not being short-living. I am not a very good programmer.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42374
    
  64
That being the case I would advise to leave the issues of Java concurrency until you have firmer grounding in the language, or to learn about them in a more structured way by working through one of the eminent book (Wong/Oaks or Goetz et al.).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Volatile in Thread