i read in core java book that each object that has synchronized methods has counter that is set to the number of synchronized methods in the object .when a thread has the lock of the object(after it called a synchronized method) then this counter will be decreased by one for each synchrized method termination after calling it by this thread and the lock will be released after decrasing this counter to zero. the question is what if the thread owning the loch didn't call all the synchronized methods .will the lock be not released?
hi, you misunderstood me. My question is when a thread locking the object release the lock is that after calling the last synchronized method it call, and is the object count the synchronized methods the thread having the lock call and if so how the object know how many synchronized methd the thread will call or the thread relesaes the lock after each invocation of a synchronized method?
mohamed hamdy, Some people, to explain what happens when you enter a synchronized block or method, use a variable or a counter that is incremented or decremented... They say that when a thread gets a lock on an object, this happens only if this thread calls, at least, one synchronized method or statement, that counter will be incremented. Only when the thread that owns the lock finish the execution of the synchronized statement or method, or something abnormal makes it release the lock, this variable will be decremented. If another thread T calls anyother, or the same, synchronized method, T will check this counter, if the counter is 1, there is already somebody in the 'room', if the counter is 0, he is free to enter, make the counter count 1 (because he is in there now) when he is done, make the counter count 0. As I said, this is only to help understanding of synchronized method and statements. But it is not so easy to do this in a multi processor context. Hope it helps. Please, correct me if I'm wrong.
i've read these phrase in a distributed document (study notes for preparing for SCJP 1.2 exam which i'm preparing for) i can't believe these phrases ,but can anyone comment. there's what i've read: Every object has a lock (for every synchronized code block). At any moment, this lock is controlled by at most one thread. A single thread can obtain multiple locks on multiple objects (or on the same object) A thread owning the lock of an object can call other synchronous methods on the same object. (this is another lock) Other threads can't do that. They should wait to get the lock. //////// when i read these again,i become confused again.
hi, hope this helps synchronized (o) {...} o is an object (any kind). Any thread entering this synchronized block will hold the lock for object o. No other thread will be allowed to enter this block, or any other block monitored by this object (o), until this thread leaves the block, releasing the lock on o. This includes any other sync block on the same object o. When you synchronize an entire method, you use 'this' as the lock object.
Their is only one lock per object. It can only be held once. It does not matter how many synchronized blocks exist on a single class, they all refer to the same lock, which can be locked only once. Once you have the lock on an object, you can safely enter any and all of its synchronized blocks without reacquiring the same lock. Once you leave all the synchronized blocks you entered, the lock is released. It may use a counter to keep track, or it may not, but this does not matter.
Post by:autobot
I hired a bunch of ninjas. The fridge is empty, but I can't find them to tell them the mission.
a bit of art, as a gift, the permaculture playing cards