Synchronization

If there are 2 threads running simultaneously in and are executing 2 synchronized methods is it possible?
[112 byte] By [vijay24a] at [2007-10-3 4:52:15]
# 1
Yes on different objects.
kajbja at 2007-7-14 22:57:01 > top of Java-index,Java Essentials,Java Programming...
# 2
can u explain this in detail
vijay24a at 2007-7-14 22:57:01 > top of Java-index,Java Essentials,Java Programming...
# 3

Class synClass{

牋牋牋牋牋

synchronize public void synMethod1(){...牋牋牋牋牋牋

......................

}

牋synchronize public void synMethod2(){...

牋牋牋爙牋?br>}

public class caller{

牋牋牋牋public static void main(String ar[]){

牋牋牋牋牋

牋synClass ref1 = new synClass();

牋牋牋牋牋

牋synClass ref2 = new synClass();

牋牋牋牋牋

牋////////simultanuous call////////////////

牋牋牋牋牋

牋 ref1.synMethod1();

牋牋牋牋牋

牋 ref2.synMethod2();

}

}

jeff_marsha at 2007-7-14 22:57:01 > top of Java-index,Java Essentials,Java Programming...
# 4

> Class synClass{

>

> synchronize public void

> synMethod1(){...

>......................

>

>synchronize public void synMethod2(){...

>

> }

> }

>

> public class caller{

> public static void main(String ar[]){

>

> synClass ref1 = new synClass();

>

> synClass ref2 = new synClass();

>

> ////////simultanuous call////////////////

>

>ref1.synMethod1();

>

>ref2.synMethod2();

>

>}

Message was edited by:

hiwa

hiwaa at 2007-7-14 22:57:01 > top of Java-index,Java Essentials,Java Programming...
# 5
class A{synchronized void mehod1(){}synchronized void method2(){}}class B extends A{A a1=new A();A a2=new A();a1.method1();a2.method2();}is it possible,if yes can u explain me
vijay24a at 2007-7-14 22:57:01 > top of Java-index,Java Essentials,Java Programming...
# 6

> class A

> {

> synchronized void mehod1(){}

> synchronized void method2(){}

> }

> class B extends A

> {

> A a1=new A();

> A a2=new A();

> a1.method1();

> a2.method2();

> }

> is it possible,if yes can u explain me

Yes, it is possible.

An object of class B has three A objects:

a1, a2 and its own super class part.

hiwaa at 2007-7-14 22:57:01 > top of Java-index,Java Essentials,Java Programming...
# 7

> class A

> {

> synchronized void mehod1(){}

> synchronized void method2(){}

> }

> class B extends A

> {

> A a1=new A();

> A a2=new A();

> a1.method1();

> a2.method2();

> }

> is it possible,if yes can u explain me

You don't even have two threads here.

Synchronization obtains a lock. You either specify the lock explicitly, as int synchronized (someObj) { /* do stuff */ }

or, when you declare a method sycned, such as synchronized void foo() { /* do stuff */ }

then the lock is the current object. It's as if you did synchronized (this) around the body of the object.

Now, when one thread has a lock, no other thread can obtain it until the first thread releases it, either by exiting the sync block or method, or by calling wait().

However, another thread can obtain any other lock. So if two threads lock on two different objects, they can call the same sync methods on those objects.

Locks are associated with objcts, not methods.

jverda at 2007-7-14 22:57:01 > top of Java-index,Java Essentials,Java Programming...