aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes ENLIGHTENING, thread synchronization, concept clarification --plz help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "ENLIGHTENING, thread synchronization, concept clarification --plz help" Watch "ENLIGHTENING, thread synchronization, concept clarification --plz help" New topic
Author

ENLIGHTENING, thread synchronization, concept clarification --plz help

sarim raza
Ranch Hand

Joined: Nov 02, 2000
Posts: 232
If a thread say A, acquires the lock of an object, and this object's class has two synchronized methods and two non synchronized methods ...
Q1] Can Thread A only execute one of the synchronized methods of this object at one time ?
Q2] I know that no other thread can execute any synchronized method of the object during this time (when thread A has a lock on the object, and is implementing a synchronized method) ....
BUT
CAN OTHER THREADS EXECUTE THE NON-SYNCHRONIZED METHODS OF THIS OBJECT DURING THIS TIME, simulatenously ???
thanks
Cherry Mathew
Ranch Hand

Joined: Dec 26, 2000
Posts: 159
The theory of synchronization will help clear u r doubt.
When a thread enters synchronized method or block it tries to acquire the lock of that object.If it doesnt have the lock it waits for it. But for a non-synchronized function it wont ask for the lock and it will execute it even though the lock is with another thread.
hope this clear u r doubt
George Toronto
Ranch Hand

Joined: Dec 30, 2000
Posts: 78
Hi, sarim
Let us suppose the segment of code seems like the following:


[B]Q1] Can Thread A only execute one of the synchronized methods of this object at one time ?

That is right. Every object of a class has only one lock with a key. When a thread is excuting a sychronized method of a object, that is , the thread owns the only lock, and the other threads have to wait when they want to access the sychronized method or other sychronized methods of the object. But, the thread which is excuting a sychronized method can also access other sychronized methods without being blocked.
CAN OTHER THREADS EXECUTE THE NON-SYNCHRONIZED METHODS OF THIS OBJECT DURING THIS TIME, simulatenously ???

Of course, the thread running can access the non-sychronized methods. the non-syschronized methods can be invoked and called by any thread at any time.
You can try several examples from Khalid Book or RHE Book. By doing that, you can understand thread deep and clearly.
regds
George
 
jQuery in Action, 2nd edition
 
subject: ENLIGHTENING, thread synchronization, concept clarification --plz help