Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Synchronized block in java

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    13
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Synchronized block in java

    Hi Do not think I am asking repeated question.As I could not express my doubt clearly once again i am sending.
    -------------------------------------------------------------------------------------------------------------
    Part1
    --------------:
    Class table
    {
    Synchronized(this)
    {
    ……………………
    }
    }
    Here we got lock on object of table class.
    --------------------------------------…
    Part2:
    Class table
    {
    Customer cust=new Customer();
    Synchronized(cust)
    {
    ……………………
    }
    }
    Here we got lock on object of customer.i.e on cust
    - table class.In part1 we got lock on object of
    We get lock on table class because object of table class may try to access the synchronized block from different places simultaneously.To prevent it we got lock on object of table class in part1.
    -Now come to part2:
    Here we got lock on object of customer class
    --------------------------------------…
    My doubts:
    1)why should we lock an object in table class other than table class in part2?
    2)2)If we do not get lock on cust in part 2 then we can access synchronized block using table class
    If we lock cust then also we need object of table class to access synchronized block.
    Object of table class only needed to access synchronized block.
    Due to synchronization we prevented object of table class to access synchronized block simultaneously from different threads.
    Then why should we lock object other than table class in table class?
    3)Will object of customer class try to access the synchronized block simultaneously from different places?
    4).In part2 we got lock on object other than table class.
    In What situations getting lock in table class on an object of a class other than table is benefit of getting lock on object of customer class in part2?
    5)What is the need to lock object of customer class i.e other than table class?
    6)What damage will happen if we do not get lock on object of customer class in part2?
    --------------------------------------…

    I think meaning of all questions is same.But I expressed doubts as I got.
    I hope you answer


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Synchronized block in java

    Can you write a small simple program the compiles, executes and shows the problem you are trying to solve?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    8
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Synchronized block in java

    hi,
    Just putting in some effort to sort things out...Assuming in both part 1 and part 2 we are using synchronized blocks.
    In first case Table object is acting as a lock.
    In Second case Customer object is acting as the lock.

    The advantage we have in second approach is that we can have have mutually exclusive multiple synchronized blocks (synchronized on different lock objects) thus granting granularity in concurrency.
    We are not having lock on Customer , here Customer object is acting as a lock and we are locking only synchronize statements , so that no two threads can invoke the synchronized block ate the same time.Objects (weather customer or table ) just acts as a lock..A thread needs to acquire the lock before executing synchronized method or block and these objects just acts as a lock..
    hope things are bit clear..

Similar Threads

  1. doubt on synchronized block-2
    By me_shankara in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 10th, 2012, 08:34 AM
  2. synchronized block
    By me_shankara in forum Threads
    Replies: 2
    Last Post: December 10th, 2012, 08:34 AM
  3. synchronized block in java
    By me_shankara in forum Threads
    Replies: 1
    Last Post: December 3rd, 2012, 11:04 PM
  4. Synchronized block vs Synchronized method
    By tcstcs in forum Java Theory & Questions
    Replies: 1
    Last Post: April 20th, 2011, 07:51 AM
  5. Java sliding block puzzle issues
    By Vesper in forum What's Wrong With My Code?
    Replies: 15
    Last Post: April 11th, 2011, 07:30 AM