Synchronized Question
Hi All,
I have method A and method B with some input parameters
I have methods 1 and method 2 which call: method A and method B
i.e.
method 1 calls method A and method B
method 2 calls method A and method B
method 1 and method 2 are synchronized
method A, method B are NOT synchornized
When a thread is in method 1, another thread calling method 2 is BLOCKED !
why? because these synchornized method calls the same methods? is it possible to call concurrently method1 and method 2 ?
Thanks
T
[565 byte] By [
tlloretia] at [2007-11-27 11:43:17]

> When a thread is in method 1, another thread calling
> method 2 is BLOCKED !
>
> why?
M1 and m2, assuming they're being called on the same object, are obtaining the same lock. This is the whole point of syncing.
jverda at 2007-7-29 17:49:07 >

Sorry but i dont understand it ....
method 1 and method 2 are public static synchronized methods of the same object yes, but they are different methods
it means that if you synchronized a method of a class you cannot call concurrently other synchronized methods of that class? or you cannot call concurrently each syncronized method independently?
thanks!
T
> it means that if you synchronized a method of a class
> you cannot call concurrently other synchronized
> methods of that class?
Once a thread enters one synchronized method of an object, no other thread will be able to enter any synchronized method of that object until the lock is released.
~
> Sorry but i dont understand it ....
>
> method 1 and method 2 are public static synchronized
> methods of the same object yes
No, they're not. If they're static, then they're associated with the class, not with any objet.
>, but they are
> different methods
Doesn't matter.
When you enter a sync block or method, you obtain a lock. No other thread can obtain that lock until you release it. In the case of a static sync method, the lock is the one associated with the class object.
class Foo {
static synchronized void m1() {}
static synchronized void m2() {}
}
When any thread is in either of those methods it holds the lock for Foo.class, so no other thread that needs that lock can get it. That means no other thread can enter either method.
> it means that if you synchronized a method of a class
> you cannot call concurrently other synchronized
> methods of that class?
For static methods, you can't concurrently call any other static synced methods on that class.
For non-static methods, you cannot concurrently call any other non-static synced methods on the same instance.
> or you cannot call
> concurrently each syncronized method independently?
No, it doesn't work that way.
jverda at 2007-7-29 17:49:07 >

And why this restriction? I mean,,, why it is not possible to call 2 DIFFERENT synchronized methods of the same class concurrently?
Just to know it ...
Thanks!
> And why this restriction? I mean,,, why it is not
> possible to call 2 DIFFERENT synchronized methods of
> the same class concurrently?
It's not a restriction. It's part of the point of the design of the language. You usually don't want to call two synced methods simultaneously.
If you need to, you can do it though.
synchronized void m1() { /* body */ }
// is the sams as
void m1() {
synchronized(this) { /* body */ }
}
So you can do this: void m1() {
synchronized(lock1) { /* body */ }
}
void m2() {
synchronized(lock2) { /* body */ }
}
jverda at 2007-7-29 17:49:07 >
