This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread Synchronization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread Synchronization" Watch "Thread Synchronization" New topic
Author

Thread Synchronization

Rishi Singh
Ranch Hand

Joined: Dec 09, 2000
Posts: 321
This question is to mahaanna any anyone can contribute to it.I tried the code below and got the output as
i=0Thread-1Static Sysnc print
i=1Thread-1Static Sysnc print
i=2Thread-1Static Sysnc print
i=3Thread-1Static Sysnc print
i=4Thread-1Static Sysnc print
i=0Thread-2 Sysnc print
i=1Thread-2 Sysnc print
i=2Thread-2 Sysnc print
i=3Thread-2 Sysnc print
i=4Thread-2 Sysnc print
THE CODE:

Now the querry is in her post mahaanna has written, if one Thread executes the static Synchronized method on a instance of a class it means it has its class lock and no other Thread can possibly execute any method on that object, or any other objectof that class.Then how come the above output.Is there something to do with the scheduler,or the inference can be that class lock exists independent of the object lock? Mahaanna please help?
[ Ajith added code tags for better readability ]
[This message has been edited by Ajith Kallambella (edited December 26, 2000).]
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
t1 starts and has the lock until it completes it's 5 loops. Then it gives up the lock and t2 starts and completes it's 5 loops.
Did you really mean to use the same PrintUpto5 instance for both threads?? You created 2 but only used 1.


"JavaRanch, where the deer and the Certified play" - David O'Meara
Rishi Singh
Ranch Hand

Joined: Dec 09, 2000
Posts: 321
dear Cindy,
Iam creating two threads ,using t1 to acess static Synchronized method and t2 to acess synchronized method , both of the same PrintUpto5 instance?
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
Rishi, I am not sure what you are confused about. It seems like you stated right in your summary. Since there is a lock on the class, The Static method will print until the loop is done. I upped the amount to 150 iterations to get a better look at it and directed it to a file, and it doesn't give up the lock until all 150 loops are done and then it releases the lock for the other loop to start.
However, since you are creating this with the same object PrintUpTo5, you could even take out the Static part and you will get the same output. That is because both methods are synchronized and since it is the same object, the lock will hold.
However, if you use the two objects you created in main, and take out the static, you will get mixed results becuase while you are locking the object, you have two different objects, so the locks do not conflict.
Likewise if you take out the synchronized keyword, you will get mixed results since no objects are locked in the first place.
Bill
Ashish Agarwal
Ranch Hand

Joined: Dec 02, 2000
Posts: 36
Dear Bill and Rishi,
I ran the code 2 times as it is and got the following outputs
i=0Thread-1 Sysnc print
i=1Thread-1 Sysnc print
i=0Thread-0 Static Sysnc print
i=2Thread-1 Sysnc print
i=1Thread-0 Static Sysnc print
i=3Thread-1 Sysnc print
i=2Thread-0 Static Sysnc print
i=4Thread-1 Sysnc print
i=3Thread-0 Static Sysnc print
i=4Thread-0 Static Sysnc print

i=0Thread-0 Static Sysnc print
i=1Thread-0 Static Sysnc print
i=0Thread-1 Sysnc print
i=2Thread-0 Static Sysnc print
i=1Thread-1 Sysnc print
i=3Thread-0 Static Sysnc print
i=2Thread-1 Sysnc print
i=4Thread-0 Static Sysnc print
i=3Thread-1 Sysnc print
i=4Thread-1 Sysnc print
What I think that it is not necessary that if a Thread is having a class lock via static synchronized method, schedular will run that thread to completion. It depends on the schedular, if another thread is trying to acquire the lock on an object of the same class the schedular may transfer first thread to runnable
pool and start executing the second thread as class and object lock work independently. This is what happening in both the above cases.
If you remove static than you will get the output as shown by Rishi as now the object is same and the thread which first acquires a lock on that object will run full iteration.
The explanation which Bill has given gives me an impression that if a thread is having a lock on a class it will run to completion and then only it will release the lock and allow
another thread to lock its object, while this is not the case as shown by above output.
I think that in that case we have to use wait() and notify() methods to get the desired output.
Please clarify if I'am wrong.
Thanks
Ashish.
Ashish Agarwal
Ranch Hand

Joined: Dec 02, 2000
Posts: 36
Bill, reply please.
Ashish Agarwal
Ranch Hand

Joined: Dec 02, 2000
Posts: 36
Bill, Please Reply.
Ashish Agarwal
Ranch Hand

Joined: Dec 02, 2000
Posts: 36
Will someone like to clear my doubt.
Thankx in advance
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
Well, I think this has nothing to do with static synchronized block. The behaviour you are observing is due to different implementations of thread scheduler. When you start two threads one after the other, both of them enters the "ready to run" state and both the threads are "eligible" to get picked by the thread scheduler. Which one runs first and whether one gets to run completely before the otherone depends entirely on the implementation of the thread scheduler.
I would also like to add the inferences made so far about the static synchronzed code are absolutely right. When a thread enters a static synchronzed block, the lock is obtained on the Class object and hence no other thread can ever execute any other synchornized method until the current thread completes the execution of the static synchronized code.
Also note that when there are two (non-static) synchronzed methods which are called separately by two different threads on a same object, the first thread that gets to run locks the object and the second thread has to wait till the first thread completes since the lock is on the same instance.
Hope that helps,
Ajith


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5370
Originally posted by Ajith Kallambella:
When a thread enters a static synchronzed block, the lock is obtained on the Class object and hence no other thread can ever execute any other synchornized method until the current thread completes the execution of the static synchronized code.

Hi Ajith
Please modify the above statment as it need some modification as other non-static methods can be executed even if yu lock the class
please visit this. and go through the threads(thread_1
& thread_2) where Maha has explained everything in great detail.
[ January 17, 2002: Message edited by: ravish kumar ]

"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
Shivaji Marathe
Ranch Hand

Joined: Jan 11, 2002
Posts: 203
What is wrong with Ajith's explanation?
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
to Ajith

the lock is obtained on the Class object and hence no other thread can ever execute any other synchornized method until the current thread completes the execution of the static synchronized code.

I think others synchronized methods can be called by other threads if they are protected by another lock.


SCJP2. Please Indent your code using UBB Code
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5370
Originally posted by Shivaji Marathe:
What is wrong with Ajith's explanation?

Originally posted by Maha
If a thread objtains a class level lock, then another thread can very well be able to run ALL methods (including the instance sync methods) EXCEPT the 'static sync' methods


by maha anna
and this is last post by Maha at this thread..
And this threda is too big.... and initial statement by Maha was same what Ajith has said here
Maha's initial stmt:
Originally posted by Maha
- Assuming Thread1 calls this staticSyncMethod() on object1, then NO OTHER
THREAD can access ANY METHOD on ANY OTHER OBJECT including the object used
by Thread1, UNTIL this Thread1 releases this Class lock.
- This means NO OTHER Thread can call any of the methods in this object,
or any method on any other NEW objects of this SAME class.



And really sorry if any one is hurted by my stmt... I did not mean to do so ... I will edit them right now
[ January 17, 2002: Message edited by: ravish kumar ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread Synchronization
 
Similar Threads
Threads
synchronized mess up
synch block again
Synchronized block
Thread Synchrinization