Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

may this situation arise in threads during dealing with volatile variable and synchronized method?  RSS feed

 
praveen kumaar
Ranch Hand
Posts: 450
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suppose i have declared a volatile variable say "count" and a synchronized method "m" (incrementing count in this method by 1).
now for simplicity i have 2 threads say "A" and "B".in the run method of both A and B i am checking some condition like below:

now suppose A owns a key for a monitor of the class where m is defined and simultaneously B comes and checks a condition of somevariable.is it possible that b comes and check for condition after A is done?How!!!

i think if count is initialized with value 0 and time during A is invoking m and B comes for a check it find count=0 but not 1 as count is volatile i.e,their is not any lock associated with it.
 
Henry Wong
author
Sheriff
Posts: 22841
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I am not completely sure if I understand your description, perhaps, you can provide an example with explanation of what you are expecting?


Having said that, there are very little (if any) cases where there is an advantage to using both synchronization and volatile variables. Generally, synchronization is enough to protect the variables (single threaded access), and there is no need to use the volatile modifier.

Henry
 
praveen kumaar
Ranch Hand
Posts: 450
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not completely sure if I understand your description, perhaps, you can provide an example with explanation of what you are expecting?

ok henry consider this example-


here consider the 22nd line i.e,


suppose any thread t1 is executing and has a key for the method call and at the same time any other thread t2 is checking the specified condition above suppose now if maxdivisor is 40 during checking and changed to 42 after method invoked by t1 now condition part is done,this time t2 got a key and invoke a method where count is 41 doesnt it sets maxdivisor to 41 now.if not why??
i am bit confused for volatile please clear this doubt..
 
Henry Wong
author
Sheriff
Posts: 22841
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
praveen kumaar wrote:
suppose any thread t1 is executing and has a key for the method call and at the same time any other thread t2 is checking the specified condition above suppose now if maxdivisor is 40 during checking and changed to 42 after method invoked by t1 now condition part is done,this time t2 got a key and invoke a method where count is 41 doesnt it sets maxdivisor to 41 now.if not why??
i am bit confused for volatile please clear this doubt..


First, your synchronization of the report() method does *not* actually do anything. The report() method is an instance method of the MaxDivisor class, and each thread  that you are creating is working with a different MaxDivisor instance. So, each thread are working with different locks -- they won't block for each other.

But to answer your question... yes, volatile variables are only atomic for one read/write operation, so it may be changed from the time the condition is checked, to the time the method is called.

Henry

PS... BTW, it may be a good idea to better indent your code. As written, it is very difficult to read.
 
praveen kumaar
Ranch Hand
Posts: 450
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so this programm can give me wrong result?
 
Henry Wong
author
Sheriff
Posts: 22841
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
praveen kumaar wrote:so this programm can give me wrong result?


Well, since I don't know what you are expecting the result to be .... I can't really say whether it would be wrong or not...

But, as mentioned... you do have a few multithreaded modifications of shared variables without correct synchronization.

Henry
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!