static synchronized

Discussion in 'Java' started by Vikram, Apr 29, 2008.

  1. Vikram

    Vikram Guest

    Hi ,
    I have a doubt regarding the synchronized keyword. If there is a
    public static synchronized method, how will the code behave, as there
    is no instance on which the "lock" can be applied.

    For example I have a class ImageCounter.java as below

    public class ImageCounter{

    private static int uploadCount;

    public static synchronized void incImagesUploaded(){
    uploadCount++;
    }

    public static synchronized int getImagesUploaded(){
    return uploadCount;
    }


    }

    There are multiple threads accessing the class.
     
    Vikram, Apr 29, 2008
    #1
    1. Advertising

  2. Vikram wrote:
    > Hi ,
    > I have a doubt regarding the synchronized keyword. If there is
    > a
    > public static synchronized method, how will the code behave, as
    > there
    > is no instance on which the "lock" can be applied.


    The Class object is locked. That is:

    >
    > For example I have a class ImageCounter.java as below
    >
    > public class ImageCounter{
    >
    > private static int uploadCount;
    >
    > public static synchronized void incImagesUploaded(){
    > uploadCount++;
    > }


    This is exactly the same as

    public static void incImagesUploaded(){
    synchronized(ImageCounter.class) {
    uploadCount++;
    }
    }

    The result is that static synchronized method are synchronized against
    each other, but not against any non-static synchronized methods.
     
    Mike Schilling, Apr 29, 2008
    #2
    1. Advertising

  3. Vikram wrote:

    |> I have a doubt regarding the synchronized keyword. If there is a
    |> public static synchronized method, how will the code behave, as there
    |> is no instance on which the "lock" can be applied.

    As Mike Schilling pointed out, this synchronizes on ImageCounter.class.

    |> For example I have a class ImageCounter.java as below
    |>
    |> public class ImageCounter{
    |>
    |> private static int uploadCount;
    |>
    |> public static synchronized void incImagesUploaded(){
    |> uploadCount++;
    |> }
    |>
    |> public static synchronized int getImagesUploaded(){
    |> return uploadCount;
    |> }

    If you are using Java 1.6, and all you need is a thread safe counter,
    you might want to use an AtomicInteger.

    --

    "I'm a doctor, not a mechanic." Dr Leonard McCoy <>
    "I'm a mechanic, not a doctor." Volker Borchert <>
     
    Volker Borchert, Apr 29, 2008
    #3
  4. Vikram

    Roedy Green Guest

    On Mon, 28 Apr 2008 22:08:51 -0700 (PDT), Vikram
    <> wrote, quoted or indirectly quoted someone who
    said :

    > I have a doubt regarding the synchronized keyword. If there is a
    >public static synchronized method, how will the code behave, as there
    >is no instance on which the "lock" can be applied.


    The class itself is an object that holds the lock.
    --

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Apr 29, 2008
    #4
  5. Vikram

    Mayur Guest

    On Apr 29, 1:06 pm, Roedy Green <>
    wrote:
    > On Mon, 28 Apr 2008 22:08:51 -0700 (PDT), Vikram
    > <> wrote, quoted or indirectly quoted someone who
    > said :
    >
    > >      I have a doubt regarding the synchronized keyword. If there is a
    > >public static synchronized method, how will the code behave, as there
    > >is no instance on which the "lock" can be applied.

    >
    > The class itself is an object that holds the lock.
    > --
    >
    > Roedy Green Canadian Mind Products
    > The Java Glossaryhttp://mindprod.com


    This is a perfect loking system if you want that one and only one
    thread should access the method at a given point on time.
     
    Mayur, Apr 29, 2008
    #5
  6. On Apr 29, 10:08 am, Vikram <> wrote:
    > Hi ,
    >       I have a doubt regarding the synchronized keyword. If there is a
    > public static synchronized method, how will the code behave, as there
    > is no instance on which the "lock" can be applied.
    >
    > For example I have a class ImageCounter.java as below
    >
    > public class ImageCounter{
    >
    >   private static int uploadCount;
    >
    >   public static synchronized void incImagesUploaded(){
    >     uploadCount++;
    >
    > }
    >
    >  public static synchronized int getImagesUploaded(){
    >   return uploadCount;
    >
    > }
    > }
    >
    > There are multiple threads accessing the class.



    Lock will be applied when ImageCounter.incImagesUploaded() is being
    used by two different threads at same time, and both try to modify
    same static member uploadCount.
     
    Jitendra Singh, Apr 29, 2008
    #6
  7. I see your point but I don't think it is a problem. Either it is
    synchronised along with all other static/non-static methods or it just
    is synchronised with only the other static methods.
     
    Chase Preuninger, Apr 30, 2008
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jerry
    Replies:
    4
    Views:
    132,042
    tonni
    Aug 11, 2010
  2. Pep
    Replies:
    6
    Views:
    29,365
  3. Knute Johnson

    synchronized static methods?

    Knute Johnson, Mar 6, 2006, in forum: Java
    Replies:
    5
    Views:
    20,745
    Knute Johnson
    Mar 7, 2006
  4. dmcreyno
    Replies:
    9
    Views:
    9,623
    Mark Space
    Jun 27, 2006
  5. ankur
    Replies:
    4
    Views:
    1,506
    Eric Sosman
    Nov 28, 2008
Loading...

Share This Page