File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Synchronization....." Watch "Synchronization....." New topic


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é

Joined: Nov 24, 2005
Posts: 14688

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

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 ... ...

Vinney Shanmugam
Ranch Hand

Joined: Aug 27, 2008
Posts: 104
Thanks you all for the wonderful replies....
I agree. Here's the link:
subject: Synchronization.....
It's not a secret anymore!