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

    Default doubt on synchronized block in java

    Hi ! my doubt is on synchronized block in java
    --------------------------------------Ö
    the general form of synchronized block is
    class table
    {
    .......
    void printTable(int n)
    {
    synchronized(object)
    {
    ......
    }
    }
    }
    --------------------------------------Ö
    1)Here object means object of any class.i.e we can lock object of any class besides the object of "table" class.
    2)if we place this in place of object it is possible to lock object of table class
    3)then how can we lock object of a class other than table?can you give example based on below programme?
    4) if we want to access a variable or method from table class we need object of table .
    if we lock object of table class it is not possible to an object of table class to access synchronized block of table class
    from two different places simultaneously.
    but if we lock object of another class,say X,how can the object of class X can access synchronized block of table class?
    because object of class X is not object of table.object of table only can access members of table.
    --------------------------------------Ö
    I think you got my doubt.
    I request you to clarify my doubt.
    --------------------------------------Ö
    import java.io.*;
    class table
    {
    void printTable(int n)
    {
    synchronized(this)
    {
    for(int i=1;i<=5;i++)
    {
    System.out.println(n*i);

    try{
    Thread.sleep(500);
    }
    catch(InterruptedException ie)
    {
    System.out.println(ie);
    }
    }
    }
    }
    }
    class MyThread1 extends Thread
    {
    table t;
    MyThread1(table t)
    {
    this.t=t;
    }
    public void run()
    {
    t.printTable(5);
    }
    }
    class MyThread2 extends Thread
    {
    table t;
    MyThread2 (table t)
    {
    this.t=t;
    }
    public void run()
    {
    t.printTable(100);
    }
    }
    class synchronizedblock1
    {
    public static void main(String args[])
    {
    table t=new table();
    MyThread1 t1=new MyThread1(t);
    MyThread2 t2=new MyThread2(t);
    t1.start();
    t2.start();
    }
    }
    ======================================
    output:
    5
    10
    15
    20
    25
    100
    200
    300
    400
    500

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default Re: doubt on synchronized block in java

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

  3. #3
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default Re: doubt on synchronized block in java

    Quote Originally Posted by me_shankara View Post
    if we lock object of table class it is not possible to an object of table class to access synchronized block of table class
    from two different places simultaneously.
    That's right. So think carefully about the blocks you want to synchronize and which lock you want to use. Over-synchronizing increases the risk for deadlocks!

    Suppose you have the class Bank with the field Account[]. The method transferMoney (int accountNumber, float amount) reads and writes on a Account, but you don't want two threads to use this method simultaneously on the same account. You don't need to lock the whole Bank or this method that try to transfer money on an other account. So the code could look:

    Java Code:
    	public void transferMoney(int accountNumber, float amount) {
    		synchronized (accounts[accountNumber]) {
    			float oldBalance = accounts[accountNumber].getBalance();
    			float newBalance = oldBalance + amount;
    			accounts[accountNumber].setBalance(newBalance);
    		}
    	}
    So lock only the object that is really necessary and the piece of code that try to read or write to this object.
    Last edited by j2me64; 12-04-2012 at 02:28 PM.

  4. #4
    me_shankara is offline Member
    Join Date
    Dec 2012
    Posts
    5
    Rep Power
    0

    Default synchronized block

    the general form of synchronized block is
    Java Code:
     class table     
        {     
        .......    
         void printTable(int n)     
        {     
        synchronized(obj)     
        {    
         ......    
         }     
        }    
         }
    Here obj means object of table class or object of any class.
    1)If we write this in place of obj we get lock on the object of table class.
    That means we canít access the synchronized block simultaneously by same object of table class.
    2) But if write other objectís name other than table class in place of obj,what it means?
    Then which object canít access the synchronized block simultaneously?

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default Re: doubt on synchronized block in java

    Please go through the Forum Rules, particularly the second paragraph. I've merged the second thread you started here.

    It's also extremely bad form to ask a new or follow-on question without first having the courtesy to reply to responses on an earlier thread.

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

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default Re: doubt on synchronized block in java

    Please see my response in the third thread you started with the same question: Synchronized block in java

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

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

  7. #7
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default Re: synchronized block

    Quote Originally Posted by me_shankara View Post
    Here obj means object of table class or object of any class.
    1)If we write this in place of obj we get lock on the object of table class.
    That means we canít access the synchronized block simultaneously by same object of table class.
    2) But if write other objectís name other than table class in place of obj,what it means?
    Then which object canít access the synchronized block simultaneously?
    In Java each object has ONE lock!

Similar Threads

  1. calling yield() method in synchronized block
    By Ash-infinity in forum New To Java
    Replies: 2
    Last Post: 12-04-2012, 05:35 PM
  2. IllegalMonitorStateException from synchronized block
    By svdeepha in forum Threads and Synchronization
    Replies: 1
    Last Post: 04-26-2010, 07:32 PM
  3. doubt about synchronized
    By simon in forum Advanced Java
    Replies: 2
    Last Post: 08-05-2007, 03:49 AM
  4. Return in the Middle of synchronized Block
    By ariak in forum Advanced Java
    Replies: 1
    Last Post: 07-26-2007, 10: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
  •