static synchronized

V

Vikram

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.
 
M

Mike Schilling

Vikram said:
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.
 
V

Volker Borchert

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.
 
R

Roedy Green

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.
 
M

Mayur

The class itself is an object that holds the lock.

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.
 
J

Jitendra Singh

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.
 
C

Chase Preuninger

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top