Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2006

Tip: Looking for answers? Try searching our database.

synchronized static methods?

Thread view: 
Knute Johnson - 06 Mar 2006 04:01 GMT
If you have a class with more than one synchronized static method, are
the methods synchronized from each other?  What object are they
synchronized on?  I know that synchronized instance methods are
synchronized on 'this' but there is no 'this' with static methods/

Signature

Knute Johnson
email s/nospam/knute/

Lee Fesperman - 06 Mar 2006 04:46 GMT
> If you have a class with more than one synchronized static method, are
> the methods synchronized from each other?  What object are they
> synchronized on?  I know that synchronized instance methods are
> synchronized on 'this' but there is no 'this' with static methods/

They're synchronized on the associated 'Class' object ... equivalent to:

 static void method()
 {
   synchronized (ClassName.class)
   {
     ...
   }
 }

Signature

Lee Fesperman, FFE Software, Inc. (http://www.firstsql.com)
==============================================================
* The Ultimate DBMS is here!
* FirstSQL/J Object/Relational DBMS  (http://www.firstsql.com)

Mike Schilling - 06 Mar 2006 06:31 GMT
>> If you have a class with more than one synchronized static method, are
>> the methods synchronized from each other?  What object are they
[quoted text clipped - 10 lines]
>    }
>  }

Someone pointed out to me once that there's nothing in the Java docs that
says explicitly "All calls to ClassName.class" will result in the same
object" [1], but the fact that static synchronization works the way it does
implies this.

1. It isn't, as far as I know, guaranteed that all calls to
Class.getMethod(), for instance, result in the same Method object.
Thomas Hawtin - 06 Mar 2006 10:59 GMT
> 1. It isn't, as far as I know, guaranteed that all calls to
> Class.getMethod(), for instance, result in the same Method object.

I should hope not. java.lang.reflect.Method objects are mutable.

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Lee Fesperman - 07 Mar 2006 01:23 GMT
> >> If you have a class with more than one synchronized static method, are
> >> the methods synchronized from each other?  What object are they
[quoted text clipped - 15 lines]
> object" [1], but the fact that static synchronization works the way it does
> implies this.

Yep, it seems that the Class object for a given class should be 'unique'. The javadocs
I'm looking at sorta imply that this is true by explicitly saying that Class objects for
each type of array are unique -- "Every array also belongs to a class that is reflected
as a Class object that is *shared* by all arrays with the same element type and number
of dimensions." (emphasis mine). The javadocs for Class.forName() also say "Returns the
Class object associated with the class or interface with the given string name.", rather
than saying "Returns a Class object".

I also think it should be unique within a given classloader but not between
classloaders, lest unexpected (and unfortunate) conflicts arise.

Synchronization of static methods provide another distinction from instance methods.
Synchronized instance methods in super/sub classes all synchronize on the same monitor
within an instance. A synchronized static method synchronizes on a different monitor
than a synchronized static method in its superclass.

> 1. It isn't, as far as I know, guaranteed that all calls to
> Class.getMethod(), for instance, result in the same Method object.

As Thomas mentioned, Method objects are mutable ... because of setAccessible().

Signature

Lee Fesperman, FFE Software, Inc. (http://www.firstsql.com)
==============================================================
* The Ultimate DBMS is here!
* FirstSQL/J Object/Relational DBMS  (http://www.firstsql.com)

Knute Johnson - 07 Mar 2006 02:12 GMT
Thanks very much, everybody, for your answers to my post.

Signature

Knute Johnson
email s/nospam/knute/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.