File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes need a clarification in synchronization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "need a clarification in synchronization" Watch "need a clarification in synchronization" New topic
Author

need a clarification in synchronization

Arul Jose
Ranch Hand

Joined: Jan 14, 2005
Posts: 131


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
Sheriff

Joined: Sep 28, 2004
Posts: 18117
    
  39

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Justin Yao
Greenhorn

Joined: Jun 16, 2006
Posts: 19
'synchronized' void?
We can only synchronize either a object or a class.
[ July 19, 2006: Message edited by: Yao Gao ]
Arul Jose
Ranch Hand

Joined: Jan 14, 2005
Posts: 131
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

Joined: Jul 08, 2003
Posts: 24166
    
  30

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.


[Jess in Action][AskingGoodQuestions]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

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

Joined: Jul 19, 2006
Posts: 2
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

Joined: Jul 08, 2003
Posts: 24166
    
  30

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: need a clarification in synchronization
 
Similar Threads
synchronized method(s) purpose
which can method2 can access without reference?
Thread synchronization
Synchronized blocks
Threads