Remember what we're talking about is multi-threading. It doesn't matter in which objects we do the invoking of methods, but which threads all those objects are executing in when the invocation is done (and since execution in a single thread is sequential, we cannot invoke the same method twice simultaneously in a single thread, unless we're using recursion).if a class (say class A) has two non-static 'synchronized' methods, then is it possible to access each of these 'synchronized' methods by two different object(say instance of class B and instance of class C) SIMULATANEOUSLY ?
No that's not possible. Separate JVMs running don't talk to each other unless you have programmed them explicitly to via sockets, filesystem etc. This is because they occupy totally separate segments of memory (execution space) and so don't share anything other than possibly the common runtime libraries - though they use different class loaders so have different instances of all the objects. Recall, you also can't share objects between JVMs without some clever code.The discussion we have come across from our above posts, are JVM specific. I mean, these scenarios we are talking are for One JVM.
So,please help me to know, how to maintain a 'class Lock' across multiple JVM(s)?
Originally posted by Ramesh Kumar Swarnkar:
Hi Friends,
Need to know if a class (say class A) has two 'synchronized' methods, then is it possible to access each of these 'synchronized' methods by two separate object(say instance of class B and class C) ?
As per theory in java:
Every Class has a Lock.
Every Instance has a Lock.
Every Object has a Lock. (am not sure about the instance lock and object lock; whether they are same or different !? ).
Anyway, does this theory mean:
to work on any synchronize method an object need to acquire the lock of class(?) OR object/instance(?).
Can anybody please elaborate it.
thanks in advance !!
[ June 22, 2008: Message edited by: Ramesh Kumar Swarnkar ]
I took this to mean that you wanted to call the different methods on the same instance of a class. If the methods are synchronized, they are synchronized on the class, and the will not run at the same time. If you use a synchronized block within the methods, they will run at the same time.
Originally posted by Ramesh Kumar Swarnkar:
Hi Paul,
= No.
Sorry, if my question gives that impression.May be would have been more specific.
But, from above posts I come to a conclusion that:
if a method is 'static-synchronized', then to work on this method, one has to acquire Class Lock.
But, if the method is 'synchronized', then to work this method, one has to acquire Object Lock. And this implies to 'synchronized block' also.
Originally posted by marlajee Borstone:
Please correct me if I am wrong somewhere !!!
Opportunity is missed by most people because it is dressed in overalls and looks like work - Edison. Tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
|