Herbert Maosa,Prabhu,Shashank Jha,
Maha tried the best to explain it to you . Please read each and every
word and try to understand.
When you say your sync instance method DOES NOT have a wait() method, it simply means, once the thread calling the sync method starts executing , then it doesn't give out the lock for others. So selfish thread it is .
<pre>
synchronized void someMethod(){
stmt1;
stmt2;
stmt3;
//see here . No wait(..)/sleep(..)/yield()/or any other waiting conditions
.
.
stmtn;
} </pre>
From your next paragrapgh I understand that you are pretty much confused. You are confused bet the object and the Thread.Let me try to clear things up. Read this carefully. I am trying to refine the above example (Prabhu. you are right.
I come back to you..)
In Java what do we do ?
1. We define a class definition. This is just a blueprint of the vars and methods of the real objects which we are going to create in memory. So let us define a restaurant class named
JavaRanchContinental . Let us say it has a var
place and a instance method
dineInOrdinaryRoom() and a sync method
void dineInSpecialRoom() and another method
static void dineInVeryVeryVerySpecialRoom(). So we have a class definition (blue print like foll).
<pre>
class JavaRanchContinental {
String place; //This can be anyPlace in the world
void dineInOrdinaryRoom() {}
void dineInSpecialRoom() {}
void dineInVeryVeryVerySpecialRoom() {}
}
</pre>
2. After blueprint is over you try to build restaurants all over the world. One in India, One in Japan, One in USA, One in China...austraillia...add to this. you can build any no of restaurants in any place in the world as long as you have the resources. It is like you can do
new JavaRanchInternational() as long as you have the memory in your system. So now create two objects (restaurants ) like this.
<pre>
class Test {
JavaRanchContinental inIndia = new JavaRanchContinental();
JavaRanchContinental inUSA = new JavaRanchContinental();
JavaRanchContinental inJapan = new JavaRanchContinental();
}
</pre>
3. Now you have the blueprint, built 3 real restaurants (real objects). Assume you have inagurated all your restaurants all over the world and it is open for public to dine in.
4.Read this portion carefully. Each restaurant has a Manager /some other staff also to run the business. Each restaurant has 3 types of rooms.
ordinary(1...n)/special(1...n)/veryverySpecial(1...n) rooms. Assume all the resturants have atleast 1 room in each type built already ,to make our discussion easier and clearer. Each Restaurant Manager has been handed over a
special key to open the 2nd type specialRoom. The restaurant can have 1.. to n special rooms with different decarations /different taste may be
. But the
the only one special room key can be used to open any 1...n
special rooms The Manager has this key.
5. Now people (public) are threads. The restaurent is common for all. It has a ONLY ONE key/lock in order to use the special room.This key is
does not belong to any one particular special room (Prabhu. you got it
. This key belong to the Restaurant as a whole and can be used to open any of such special rooms , but once one room is opened, the key is with the person (thread1)dining in the room, and other rooms can't be used since we have ONLY one key per restaurant (object).
6.Other ordinary rooms can be used since they don't need the key. Simillarly there is no restriction is using the special room in another restaurant. At any time the special rooms in the JavaRanchContinental in India as well as the special room in JavaRanchContinental in USA can be used SIMULTANEOUSLY. No problem, since BOTH Managers at India and USA have their own keys to use the special room in their restaurants.
7. Having said that, The JavaRanchContinetal OWNER , whoever OWNS the whole bunch of restaurants all over the world has another key (Class level key used for static sync method), which is used to open ALL the
veryverySpecial rooms in ALL the restaurants in all over the world. This owner also has only one such key. This key is used to have a GET_TOGETHER in the 3rd type VERY VERY SPECIAL room. Once the owner anounces a party , get together , then this means none of the special rooms in all the reataurants can be used, but still the ordinary rooms can be used as usual. ( I guess all people fly/travel to the party, and they appoint temp staff to look after the people who come to dine in ordinary rooms not to disappoint them
).
So when the get together party in 3rd type veryveryverySpecial room is going on , others can't use the 2nd type special rooms.
After the party is over, the owner is happpy and all staff fly back and run their business as usual.
Herbert,
I am not sure if you follow this example. I tried best to refine this example to suit the need. Try to co-oeralte this with Class object, thread, ordinat/instance sync/static sync methods.
I give one last try for you. I go straight to your quotes.
From your post My understanding is that if we have a thread say t1 and we call t1.someMethod(), t1 will not immediately execute in somemethod()
Maha's comments This is not clear. See in Java each every code is run using a Thread object. For example when the appln is loaded and started the JVM creades a USER THREAD which run the main(String[] args) method. Simillary, the therad which runs the main(String[] args) method also can create sub threads subThread1, subThread2. Which means the corresponding public void run() methods in subThread1 and subThread2 are executed.
if thread is t1 , then in order to execute the sync method in the common object you don't call ti.someMethod() . You are creating thread t1 with new and in the run() method you call the commonObject.syncMethod() got it? See the example below. I didn't compile it. Just wrote to make is visually clear.
<pre>
class HerbertThread extends Thread{
JavaRanchContinental restaurant;
HerbertThread() {
}
HerbertThread(JavaRanchContinental restaurant) {
this.restaurant = restaurant;
}
public void run() {
//restaurant.dineInOrdibnaryRoom();
restaurant.dineInspecialRoom();
//restaurant.dineInVeryVeryVeryspecialRoom();
}
}
class PrabhuThread extends Thread{
JavaRanchContinental restaurant;
PrabhuThread() {
}
PrabhuThread(JavaRanchContinental restaurant) {
this.restaurant = restaurant;
}
public void run() {
//restaurant.dineInOrdibnaryRoom();
restaurant.dineInspecialRoom();
//restaurant.dineInVeryVeryVeryspecialRoom();
}
}
class Test {
public static void main(String[] args) {
JavaRanchContinental commonRestObj = new JavaRanchContinental();
Thread t1 = new HerbertThread(commonRestObj);
Thread t2 = new PrabhuThread(commonRestObj);
t1.start();
t2.start();
}
}
</pre>
From your post :
because maybe some other thread t2 already has the lock and is executing in this thread.
Maha's comments :Almost correct. because someother thread has already got the lock of the object and is executing
the sync method
[b]From your post So this call will result in a call to the object's wait() method(which does not appear in someMethod() above) to send it to the waiting pool so that t2 finishes its processing.
Maha's comments Each object has a instance wait() method. This call in not called automatically at any time. It has to be invoked by user specifically by explicit wait() statement. when you explicitly write wait() statement int the sync block, only the current thread which runs the sync block is pulled to waiting pool. The final wait() method does the part of releasing the lock of the current object whose sync methods it executes now,pulling the thread over to the wating pool. when you do not put any wait(), the current thread just runs the stmnt1... stmntn in the above sample code and goes to dead state. So if another thread tries to run the sync method, it has to obtain the lock first, and can't get it now, and it is put back to waiting pool by the scheduler. There is no connection bet the wait() inside the sync block of code and the another thread put back to waiting pool. Whether the sync code has a wait() or not does not matter. It is not at all connected to putting the other thread to the waiting pool. I think you are conneting the simillar name wait() and other thread put back to waiting pool.
The wait() code inside the sync block simply means it is for internal use of the currently running thread, which means, when there is a wait() inside the sync block, the currentlt running therad volunteers itself to give a chance to others by putting itself to the waiting pool. if there is no wait(), it simpley means, it doesn't bother others. It's work is first for it like that. So when other threads try to invoke this sync method , the WORK OF PUTTING the other thread back to waitng pool is done by the scheduler/JVM and it is done perfectly, whether there is a wait() code inside the sync method or not.
From your post: yea I am confused here. But how did t1 then execute wait() on this object, since it had to be the owner first, yet we know the real owner is the currently executing t2 ?
Maha's comments I hope all previous explanation will help you to answer this. (really confused statement..)
From your post: Also my understanding is that a thread will acquire the lock to an object atleast twice inorder to execute a sync method.
Maha's comments No. obtaining the object's lock once itself enough to execute the sync method.
Prabhu,
Yes.yor are right. The object's lock is for the object as a whole, not on any sync method basis. I changed our story accordingly. Also I made up the story such that
special room means really special. You have real
privacy ,no nearby tables in the same room
like that. I still maintained the restaurant as the common object, and the restaurant has a only one special key for any of the special rooms, and after getting the key from the manager, you have the real privacy room where you enjoy your special meal with your special person
Does the thread story sound good to you now? Did you read this post?. This is for you also.
Shashank Jha,
Prabhu had explained it. except when notify() is called 'even the thread scheduler doesn't know ' part. I think he means event he thread scheduler doesn't know which onw to pick statically. But it is the thread scheduler who picks up one thread among the all waitng threads in the pool. Just to make it clear.
*************The End
***********************************
I am going to have a very strong
coffee now.
regds
maha anna
[This message has been edited by maha anna (edited May 17, 2000).]