Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > static synchronized

Reply
Thread Tools

static synchronized

 
 
Vikram
Guest
Posts: n/a
 
      04-29-2008
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.
 
Reply With Quote
 
 
 
 
Mike Schilling
Guest
Posts: n/a
 
      04-29-2008
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.


 
Reply With Quote
 
 
 
 
Volker Borchert
Guest
Posts: n/a
 
      04-29-2008
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 <(E-Mail Removed)>
"I'm a mechanic, not a doctor." Volker Borchert <(E-Mail Removed)>
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      04-29-2008
On Mon, 28 Apr 2008 22:08:51 -0700 (PDT), Vikram
<(E-Mail Removed)> 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
 
Reply With Quote
 
Mayur
Guest
Posts: n/a
 
      04-29-2008
On Apr 29, 1:06*pm, Roedy Green <(E-Mail Removed)>
wrote:
> On Mon, 28 Apr 2008 22:08:51 -0700 (PDT), Vikram
> <(E-Mail Removed)> 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.
 
Reply With Quote
 
Jitendra Singh
Guest
Posts: n/a
 
      04-29-2008
On Apr 29, 10:08*am, Vikram <(E-Mail Removed)> 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.
 
Reply With Quote
 
Chase Preuninger
Guest
Posts: n/a
 
      04-30-2008
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Synchronized Block v.s. Synchronized Method Jerry Java 4 08-11-2010 02:34 PM
Question on synchronized and non synchronized methods ankur Java 4 11-28-2008 09:56 PM
synchronized block in synchronized static method dmcreyno Java 9 06-27-2006 07:43 PM
synchronized static methods? Knute Johnson Java 5 03-07-2006 02:12 AM
Use of synchronized variables over synchronized methods? Pep Java 6 08-15-2005 01:29 PM



Advertisments