This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Threads and Synchronization and the fly likes Synchronization..... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Synchronization....." Watch "Synchronization....." New topic
Author

Synchronization.....

Vinney Shanmugam
Ranch Hand

Joined: Aug 27, 2008
Posts: 104
When a Thread1 is executing a synchronized methodA in a class A, Is it possible for another Thread2 to access another non-synchronized methodB in class A?

I dont think it's possbile, because the instance is already locked by Thread1 and with that instance only, you can invoke the mehtodB, which is not possbile, as it is already locked. But is my understanding correct?
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Non-synchronized methods do not check the lock status, so they can be called at any time.


[My Blog]
All roads lead to JavaRanch
Vinney Shanmugam
Ranch Hand

Joined: Aug 27, 2008
Posts: 104
So, object lock is a sort of a flag which will be checked only when synchronized keyword is used in the code?

I thought, the whole object is locked/encapsulated from access for some time.
Sujeet Kumar Jaiswal
Greenhorn

Joined: Mar 07, 2010
Posts: 17
Locking means prohibiting multiple threads to execute a synchronized method or a synchronized block at same time. This happens by lending the object lock to the currently running thread. But still that object can be used to invoked any method by different threads.

Locking doesn’t mean that the object cannot be used to invoke a method. As Christophe also said "Non-synchronized methods do not check the lock status"

The object can be used to invoke methods by different threads:

1- If the method called is non-syncronized, it will run.
2- If the method called is synchronized and already a thread is running it, then the calling thread will wait to acquire the object lock.
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
To understand synchronization, focus more on the objects involved and their
lock status rather than the method. The goal is to protect object state as the
JVM shares the CPU among threads; preventing multiple threads from operating
simultaneously on the same object. This does not mean that only one copy of
the method is running. Rather, it means that each copy of the method must
lock and operate on a different object.

Methods are not blocked, nor is access to any object. Non-synchronized code can
trash an object's state even while a synchronized method is holding its lock. This is
why it's so important that all changes to critical objects be done in synchronized code,
so locks are obtained before any changes are made. Missing one can cause trouble.

Jim ... ...


BEE MBA PMP SCJP-6
Vinney Shanmugam
Ranch Hand

Joined: Aug 27, 2008
Posts: 104
Thanks you all for the wonderful replies....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Synchronization.....