Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > synchronized block in synchronized static method

Reply
Thread Tools

synchronized block in synchronized static method

 
 
dmcreyno
Guest
Posts: n/a
 
      06-26-2006
I recently stumbled across this idiom.

public static synchronized int getThreadCountMax()
{
synchronized (ARTClientThread.class)
{
return threadCountMax;
}
}

Its a synchronized static method with a synchronized block. Looks
redundant to me. Doesn't the synchronized static method use the class
object?

 
Reply With Quote
 
 
 
 
Thomas Fritsch
Guest
Posts: n/a
 
      06-26-2006
dmcreyno wrote:
> I recently stumbled across this idiom.
>
> public static synchronized int getThreadCountMax()
> {
> synchronized (ARTClientThread.class)
> {
> return threadCountMax;
> }
> }
>
> Its a synchronized static method with a synchronized block. Looks
> redundant to me.

It is just as dumb as writing this:
public static int getThreadCountMax()
{
synchronized (ARTClientThread.class)
{
synchronized (ARTClientThread.class)
{
return threadCountMax;
}
}
}

> Doesn't the synchronized static method use the class
> object?

I does.

--
Thomas
 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      06-26-2006
dmcreyno wrote:
> I recently stumbled across this idiom.
>
> public static synchronized int getThreadCountMax()
> {
> synchronized (ARTClientThread.class)
> {
> return threadCountMax;
> }
> }
>
> Its a synchronized static method with a synchronized block. Looks
> redundant to me. Doesn't the synchronized static method use the class
> object?


It does - but it's own class. If this code is not in class
ARTClientThread then it has different semantics than the version without
the block.

Kind regards

robert

 
Reply With Quote
 
Chris Uppal
Guest
Posts: n/a
 
      06-26-2006
dmcreyno wrote:

> public static synchronized int getThreadCountMax()
> {
> synchronized (ARTClientThread.class)
> {
> return threadCountMax;
> }
> }
>
> Its a synchronized static method with a synchronized block. Looks
> redundant to me. Doesn't the synchronized static method use the class
> object?


If getThreadCountMax() is a method at the outer level of ARTClientThread then
it is redundant.

If it's a member of any other class (including classes nested inside
ARTClientThread) then it's not redundant. In that case, depending on the
design of the locking strategy, it could be any of: wasteful, absolutely
necessary, or an invitation to create deadlocks.

-- chris


 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      06-26-2006


dmcreyno wrote On 06/26/06 12:59,:
> I recently stumbled across this idiom.
>
> public static synchronized int getThreadCountMax()
> {
> synchronized (ARTClientThread.class)
> {
> return threadCountMax;
> }
> }
>
> Its a synchronized static method with a synchronized block. Looks
> redundant to me. Doesn't the synchronized static method use the class
> object?


If the method is part of the ARTClientThread class, the
same Class object is locked redundantly.

If the method is part of some other class, that class'
Class object and ARTClientThread.class are distinct and both
are locked once each. This isn't "redundant," but it seems
"useless."

Has this code been victimiz-- er, "improved" by one of
those semi-automatic refactoring tools? The gadgets that
take the thought out of coding and produce thoughtless code?

--
http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
dmcreyno
Guest
Posts: n/a
 
      06-26-2006
As others have asked, it is a method on ARTClientThread. So, redundant
it is.
dmcreyno wrote:
> I recently stumbled across this idiom.
>
> public static synchronized int getThreadCountMax()
> {
> synchronized (ARTClientThread.class)
> {
> return threadCountMax;
> }
> }
>
> Its a synchronized static method with a synchronized block. Looks
> redundant to me. Doesn't the synchronized static method use the class
> object?


 
Reply With Quote
 
Chris Uppal
Guest
Posts: n/a
 
      06-27-2006
Red Orchid wrote:

> Therefore,
> synchronized block will not be required with the example.


[This is in addition to what Chris Smith has already explained]

Consider this:

privant int value;

public int getValue() { return value; }

public void aMethod()
{
while (true)
doSomethingWith(getValue());
}

Since getValue() is not synchronised, and uses no sychronisation blocks, the
compiler (which really means JITer) is perfectly within its right to first
inline the method call, as if the original Java source was:

public void aMethod()
{
while (true)
doSomethingWith(value);
}

and then to "notice" that value does not change during the loop, and to
optimise the field access into a local variable access:

public void aMethod()
{
int __xyz123 = value;
while (true)
doSomethingWith(__xyz123);
}

So, in another thread /does/ change value, the loop will never "see" the new
value.

Synchronisation acts not only to control the (potentially very loose) coupling
between the state of RAM seen by different CPUs, but also to inform the
optimiser of the limits to what it is allowed to assume.

-- chris


 
Reply With Quote
 
Mark Space
Guest
Posts: n/a
 
      06-27-2006
Chris Smith wrote:
> Mark Space <(E-Mail Removed)> wrote:
>> dmcreyno wrote:
>>> As others have asked, it is a method on ARTClientThread. So, redundant
>>> it is.

>> That's wild. Does Java sychronized keyword have code to check "don't
>> lock this if I have already locked this?" 'Cause otherwise it's an
>> instant deadlock...

>
> Locks are recursive in Java; hence, no deadlock.
>


I don't see how recursion would help, but I'll take your word on the no
deadlock bit.

Do you mean "able to be used in recursive algorithms" maybe? I think
that might be re-entrant or something.
 
Reply With Quote
 
Chris Smith
Guest
Posts: n/a
 
      06-27-2006
Mark Space <(E-Mail Removed)> wrote:
> Chris Smith wrote:
> > Locks are recursive in Java; hence, no deadlock.
> >

>
> I don't see how recursion would help, but I'll take your word on the no
> deadlock bit.
>
> Do you mean "able to be used in recursive algorithms" maybe? I think
> that might be re-entrant or something.


A recursive lock is a lock that keeps track of who is holding it, and a
lock count. When the holder tries to grab the lock a second time, it
simply increments that counter. When the holder releases the lock,
then, it decrements that counter. Only when the counter reaches zero is
the lock actually released.

This does solve this specific deadlock problem. Of course, it's still
possible to write deadlocks in Java, but it does require two locks.

--
Chris Smith - Lead Software Developer / Technical Trainer
MindIQ Corporation
 
Reply With Quote
 
Mark Space
Guest
Posts: n/a
 
      06-27-2006
Chris Smith wrote:
> Mark Space <(E-Mail Removed)> wrote:
>> Chris Smith wrote:
>>> Locks are recursive in Java; hence, no deadlock.
>>>

>> I don't see how recursion would help, but I'll take your word on the no
>> deadlock bit.
>>
>> Do you mean "able to be used in recursive algorithms" maybe? I think
>> that might be re-entrant or something.

>
> A recursive lock is a lock that keeps track of who is holding it, and a
> lock count. When the holder tries to grab the lock a second time, it
> simply increments that counter. When the holder releases the lock,
> then, it decrements that counter. Only when the counter reaches zero is
> the lock actually released.


Ok, first time I've heard that term, applied to mutex or semaphores
anyway. Thanks!
 
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
static synchronized method Ross Java 9 07-29-2011 10:01 PM
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
Fo:Block can you check to see if a block contains any text by using the block id? morrell XML 1 10-10-2006 07:18 PM
Use of synchronized variables over synchronized methods? Pep Java 6 08-15-2005 01:29 PM



Advertisments