Results 1 to 2 of 2
  1. #1
    me_shankara is offline Member
    Join Date
    Dec 2012
    Posts
    5
    Rep Power
    0

    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
    --------------:
    Java Code:
    Class table
    {
    Synchronized(this)
    {
    
    }
    }
    Here we got lock on object of table class.
    --------------------------------------
    Part2:
    Java Code:
    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
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,453
    Rep Power
    20

    Default Re: Synchronized block in java

    Please see my responses at #2 and #5 in your previous thread: doubt on synchronized block in java

    Any more multiposting and you will be banned for a period. There will be no further warning.

    db

    THREAD CLOSED
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. doubt on synchronized block in java
    By me_shankara in forum Threads and Synchronization
    Replies: 6
    Last Post: 12-20-2012, 01:38 PM
  2. calling yield() method in synchronized block
    By Ash-infinity in forum New To Java
    Replies: 2
    Last Post: 12-04-2012, 06:35 PM
  3. IllegalMonitorStateException from synchronized block
    By svdeepha in forum Threads and Synchronization
    Replies: 1
    Last Post: 04-26-2010, 08:32 PM
  4. Return in the Middle of synchronized Block
    By ariak in forum Advanced Java
    Replies: 1
    Last Post: 07-26-2007, 11:24 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •