• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

synchronization quesion

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If a thread doesn't call a synchronized method then it doesn't acquire the lock.
 
mohamed hamdy
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
mohamed hamdy
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
He repaced his skull with glass. So you can see his brain. Kinda like this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic