You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by "Cox, Charlie" <cc...@cincom.com> on 2001/06/12 21:35:39 UTC

RE: static,synchronized and classloaders(was:Java Question)

Now is this true for when I have multiple contexts mapped to the same
docbase?  I have seen on this list that Tomcat loads the classes(in
web-inf/classes) independently for each context and passing MyObject class
instance between contexts is not the same 'MyObject' class in the second
context even though it is the same code. Since it is not the same class I
would assume that I would have two instances of a static method/field. And
if it is synchronized on a static member(or the class itself) it would only
be synchonized for this context.

I'm really not sure what I would have if the class is static. 

Thoughts?
Charlie

-----Original Message-----
From: Pae Choi [mailto:paechoi@earthlink.net]
Sent: Tuesday, June 12, 2001 2:42 PM
To: tomcat-user@jakarta.apache.org
Subject: Re: Java Question


When you access the 'synchronized' static method, it locks its class.
so this will ensure the thread-safe access. Otherwise, the result is
unknown.


Pae


> I have looked all over and can't find the answer to this simple question.
> If you use a static method, do you have to synchronize it in case other
> people may access it at the same time.  For example, I have a static
Utility
> class to do date calculations.  The method Utility.getMonth(String date)
> takes in a full date string, parses it, and returns just the month value.
> If 5 different people all using the website attempt to use
> Utility.getMonth(String date) at the same time for different dates, will
it
> return the right results?  If not, do I have to synchronize it or
something
> in case multiple users attempt to access it?
> 
> I know this is not really related to tomcat, but since I am using tomcat,
> and everyone else using tomcat is also a java developer, I figured this is
> the best place I can ask.
> 
> Thanks for any help!!!
> 
> Brandon
> 

Re: static,synchronized and classloaders(was:Java Question)

Posted by Luba Powell <lu...@bellatlantic.net>.
RE: static,synchronized and classloaders(was:Java Question)If your context points to another virtual machine - yes. 
On the same virtual machine static is static

  ----- Original Message ----- 
  From: Cox, Charlie 
  To: 'tomcat-user@jakarta.apache.org' 
  Sent: Tuesday, June 12, 2001 3:35 PM
  Subject: RE: static,synchronized and classloaders(was:Java Question)


  Now is this true for when I have multiple contexts mapped to the same docbase?  I have seen on this list that Tomcat loads the classes(in web-inf/classes) independently for each context and passing MyObject class instance between contexts is not the same 'MyObject' class in the second context even though it is the same code. Since it is not the same class I would assume that I would have two instances of a static method/field. And if it is synchronized on a static member(or the class itself) it would only be synchonized for this context.

  I'm really not sure what I would have if the class is static. 

  Thoughts? 
  Charlie 

  -----Original Message----- 
  From: Pae Choi [mailto:paechoi@earthlink.net] 
  Sent: Tuesday, June 12, 2001 2:42 PM 
  To: tomcat-user@jakarta.apache.org 
  Subject: Re: Java Question 



  When you access the 'synchronized' static method, it locks its class. 
  so this will ensure the thread-safe access. Otherwise, the result is 
  unknown. 



  Pae 



  > I have looked all over and can't find the answer to this simple question. 
  > If you use a static method, do you have to synchronize it in case other 
  > people may access it at the same time.  For example, I have a static Utility 
  > class to do date calculations.  The method Utility.getMonth(String date) 
  > takes in a full date string, parses it, and returns just the month value. 
  > If 5 different people all using the website attempt to use 
  > Utility.getMonth(String date) at the same time for different dates, will it 
  > return the right results?  If not, do I have to synchronize it or something 
  > in case multiple users attempt to access it? 
  > 
  > I know this is not really related to tomcat, but since I am using tomcat, 
  > and everyone else using tomcat is also a java developer, I figured this is 
  > the best place I can ask. 
  > 
  > Thanks for any help!!! 
  > 
  > Brandon 
  > 


Re: static,synchronized and classloaders(was:Java Question)

Posted by Luba Powell <lu...@bellatlantic.net>.
RE: static,synchronized and classloaders(was:Java Question)Also, look if identical files are generated based on 2 different context:
abc.MyClass is not the same as xyx.MyClass.  If 2 class loaders 
load from identical class files - JVM verifier should throw the exception
before a second object is created. If this doesn't happen - this is a 
serious bug.

Also it is not possible to synchronize on the class - only on the method
and a variable that is an object
  ----- Original Message ----- 
  From: Cox, Charlie 
  To: 'tomcat-user@jakarta.apache.org' 
  Sent: Tuesday, June 12, 2001 3:35 PM
  Subject: RE: static,synchronized and classloaders(was:Java Question)


  Now is this true for when I have multiple contexts mapped to the same docbase?  I have seen on this list that Tomcat loads the classes(in web-inf/classes) independently for each context and passing MyObject class instance between contexts is not the same 'MyObject' class in the second context even though it is the same code. Since it is not the same class I would assume that I would have two instances of a static method/field. And if it is synchronized on a static member(or the class itself) it would only be synchonized for this context.

  I'm really not sure what I would have if the class is static. 

  Thoughts? 
  Charlie 

  -----Original Message----- 
  From: Pae Choi [mailto:paechoi@earthlink.net] 
  Sent: Tuesday, June 12, 2001 2:42 PM 
  To: tomcat-user@jakarta.apache.org 
  Subject: Re: Java Question 



  When you access the 'synchronized' static method, it locks its class. 
  so this will ensure the thread-safe access. Otherwise, the result is 
  unknown. 



  Pae 



  > I have looked all over and can't find the answer to this simple question. 
  > If you use a static method, do you have to synchronize it in case other 
  > people may access it at the same time.  For example, I have a static Utility 
  > class to do date calculations.  The method Utility.getMonth(String date) 
  > takes in a full date string, parses it, and returns just the month value. 
  > If 5 different people all using the website attempt to use 
  > Utility.getMonth(String date) at the same time for different dates, will it 
  > return the right results?  If not, do I have to synchronize it or something 
  > in case multiple users attempt to access it? 
  > 
  > I know this is not really related to tomcat, but since I am using tomcat, 
  > and everyone else using tomcat is also a java developer, I figured this is 
  > the best place I can ask. 
  > 
  > Thanks for any help!!! 
  > 
  > Brandon 
  >