Results 1 to 4 of 4
  1. #1
    markthien is offline Member
    Join Date
    Jul 2009
    Posts
    4
    Rep Power
    0

    Wink correct argument to put in synchronized method

    Hi guys,

    I am wondering what is the proper argument to pass in a synchronized method:
    Java Code:
    public void method1() { 
        synchronized( this ) { 
            ....
        }
    }
    
    public void method1() { 
        synchronized( MyClass.class) { 
            ....
        }
    }
    
    public void method1() { 
        synchronized( MyClass.class) { 
            ....
        }
    }
    
    public void method1() { 
        A objecta=new A();
        synchronized( objecta) { 
            ....
        }
    }
    Please kindly advice. Thanks in advance!

    Cheers,
    Mark Thien
    Last edited by sunde887; 08-27-2011 at 05:27 PM. Reason: Added code tags, [code]...[/code]

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    786
    Rep Power
    5

    Default

    We cannot tell you which one is right. It depends on what do you want to do :)

    Intrinsic Locks and Synchronization (The Java™ Tutorials > Essential Classes > Concurrency)

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,607
    Rep Power
    23

    Default

    what is the proper argument to pass in a synchronized method
    There is no requirement to pass any arguments to a synchronized method.

  4. #4
    aburnett397 is offline Member
    Join Date
    Feb 2011
    Posts
    3
    Rep Power
    0

    Default Re: correct argument to put in synchronized method

    Synchronized segments in your code can only be executed by a single thread at a time for which the same object was provided as an "argument". Therefore...

    public void method1() {
    synchronized( this ) {
    ....
    }
    }
    method1() cannot run simultaneously for a given instance. This is the same as 'public synchronized void method1() {...}'

    public void method1() {
    synchronized( MyClass.class) {
    ....
    }
    }
    method1() cannot run simultaneously for any instance

    public void method1() {
    A objecta=new A();
    synchronized( objecta) {
    ....
    }
    }
    No synchronization since each call to method1() synchronizes on a different object

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. The differences made by Synchronized method
    By dav in forum Threads and Synchronization
    Replies: 3
    Last Post: 05-25-2011, 02:49 PM
  3. Method as an argument?
    By StokedOnMe in forum New To Java
    Replies: 13
    Last Post: 09-18-2009, 06:29 AM
  4. Servlet behaving badly unless method is synchronized
    By domanows in forum Java Servlet
    Replies: 0
    Last Post: 01-29-2009, 02:37 AM
  5. [SOLVED] Non-synchronized instance method of an Object
    By piyu.sha in forum Threads and Synchronization
    Replies: 2
    Last Post: 10-06-2008, 06:35 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
  •