• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

need a clarification in synchronization

 
Arul Jose
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


We have two methods here one is synchronized and the other is not. When an object of this class is locked, only one thread can access method1() but any number of threads can access method2(). while method1() runs, method2() also can simultaneously change the value of i which is used by method1() (method1 will see a different value for i, since it was altered by method2), since it is accessible by any number of threads. Is this not making the purpose of 'synchronized' void?
[ July 19, 2006: Message edited by: Arul Jose ]
 
Henry Wong
author
Marshal
Pie
Posts: 21010
77
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Arul Jose:

We have two methods here one is synchronized and the other is not. When an object of this class is locked, only one thread can access method1() but any number of threads can access method2(). while method1() runs, method2() also can simultaneously change the value of i which is used by method1() (method1 will see a different value for i, since it was altered by method2), since it is accessible by any number of threads. Is this not making the purpose of 'synchronized' void?


As you have figured out, declaring a method as synchronized does not make the method thread safe. It must be done correctly. And it must be used as it was intended.

Henry
 
Justin Yao
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
'synchronized' void?
We can only synchronize either a object or a class.
[ July 19, 2006: Message edited by: Yao Gao ]
 
Arul Jose
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It must be done correctly. And it must be used as it was intended.


Henry, do you mean, it is the responsibility of the 'coder' to be careful ?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Arul Jose:


Henry, do you mean, it is the responsibility of the 'coder' to be careful ?


Yes. He means that the person who made this one method synchronized, but not the other one, even though they both read and write "i", made a mistake. Synchronization is not a magic bullet; it's a tool which must be used correctly. Correctly, in this case, would mean making both methods synchronized.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Yao Gao:
'synchronized' void?
We can only synchronize either a object or a class.


Synchronizing a method has nothing to do with the return type of the method; it's the object that has the method that is used as the lock.

I'm moving this topic to our "Threads and synchronization" forum.
 
Abhinav Prakash
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I disagree with the fact that the code undermines the notion of word synchronized. The code written i think is correct and i will not have inconsistent value.

Each thread has its own working memory. variable 'i; in this case is the shared memory. When ever a thread sees synchrnoized it locks the object. then it loads the value of the variable from the shared memory to its working memory. The object is locked for wrting the manipulated value from the thread working memory to the shared memory. Till this transition happens no other thread will be able to write to shared memory. however other threads are still able to read the values.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Abhinav Prakash:
Till this transition happens no other thread will be able to write to shared memory.


Sorry, but not only is the above statement simply not true, even if it were true, it ignores the fact that "--i" is not an atomic operation. Without synchronization, there are possible values for "i" that are impossible if the methods are properly synchronized. Without synchronization, method2 could read the unincremented value of i, then method1 could increment i and write it to main memory, then method2 could decrement its local copy and write it back out, overwriting the value computed by method1, and giving a value one less than the original, as if method1 had never been called.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic