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?
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.
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
Joined: Aug 27, 2008
Thanks you all for the wonderful replies....
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com