Board index » jbuilder » whose lock is used in static synchronized method !?

whose lock is used in static synchronized method !?


2004-02-19 12:56:36 AM
jbuilder12
Hi,
class C {
public static synchronized void c1() {
// do something
}
public static synchronized void c2() {
// do something
}
}
Note that I can invoke functions c1 or c2 without creating the object of
type C !
Now the question is whose lock has been used in synchronized functions if
the class has never been instantiated ?!?!
Thanks
Petar
 
 

Re:whose lock is used in static synchronized method !?

What really makes me confused is that these two functions are not
synchronized on the same lock object- although they booth have synchronized
keyword. Therefore two threads can enter booth of them at the same time. The
static has the lock of "Class object" and the non-static has the lock on
"this" object.
Am I right ? It is hard to prove this, so I need confirmation.
THANKS.
class C1 {
synchronized public static void do1() {
...
}
synchronized public void do2() {
...
}
}
 

Re:whose lock is used in static synchronized method !?

On 2/18/2004 at 11:56:36 AM, Petar Banicevic wrote:
Quote
Now the question is whose lock has been used in synchronized
functions if the class has never been instantiated ?!?!
Class methods are synchronized on the Class object associated with the
class.
--
Regards,
John McGrath [TeamB]
---------------------------------------------------
Before sending me e-mail, please read:
www.JPMcGrath.net/newsgroups/e-mail.html
 

{smallsort}

Re:whose lock is used in static synchronized method !?

On 2/18/2004 at 5:18:58 PM, Petar Banicevic wrote:
Quote
What really makes me confused is that these two functions are not
synchronized on the same lock object- although they booth have
synchronized keyword. Therefore two threads can enter booth of them
at the same time. The static has the lock of "Class object" and the
non-static has the lock on "this" object.

Am I right ? It is hard to prove this, so I need confirmation.
Yes, exactly.
--
Regards,
John McGrath [TeamB]
---------------------------------------------------
Before sending me e-mail, please read:
www.JPMcGrath.net/newsgroups/e-mail.html
 

Re:whose lock is used in static synchronized method !?

Thank you !
P.
"John McGrath [TeamB]" < XXXX@XXXXX.COM >wrote in message
Quote
On 2/18/2004 at 5:18:58 PM, Petar Banicevic wrote:

>What really makes me confused is that these two functions are not
>synchronized on the same lock object- although they booth have
>synchronized keyword. Therefore two threads can enter booth of them
>at the same time. The static has the lock of "Class object" and the
>non-static has the lock on "this" object.
>
>Am I right ? It is hard to prove this, so I need confirmation.

Yes, exactly.

--
Regards,

John McGrath [TeamB]

---------------------------------------------------
Before sending me e-mail, please read:
www.JPMcGrath.net/newsgroups/e-mail.html