my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Need some help, about synchronization topic Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Need some help, about synchronization topic" Watch "Need some help, about synchronization topic" New topic
Author

Need some help, about synchronization topic

Baaziz Hamza
Greenhorn

Joined: Aug 30, 2012
Posts: 8

(Edit)
Hello there , I have a question about synchronization topic !
In a given method we can synchronize a block of code on a third party object
exm :

I hope i understood this right : while a thread is executing the synchronized block of code, it 'll lock the OtherObject, & no other thread will be able to modify that instance or call it's methods.
So I did a simple code to test :

I was expecting that while the synchronized for of Toto is running, AppCertif.test instance will be locked, & Titi won't be able to call it's method.
But I didn't get what I was expecting, instead during execution I get some

...
Iam the tested synch-on objet, by Titi
Iam the tested synch-on objet, by Toto
Iam the tested synch-on objet, by Titi
...

which shows that both threads can access the AppCertif.test methods even if we're synchronizing on it !!!

I hope someone can explains to me, what I'm missing please !
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 375
    
    1

Baaziz Hamza wrote:
I hope someone can explains to me, what I'm missing please !


Suppose you are using a pen to do an assignment. And one more person sitting besides you does not have a pen..

You will happily share the pen with him occassionally, but you will certainly not want him to take away the pen out of the country unless you are done with your job..

Apply the same with Threading.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

the basic key point here to understand this behavior is that if 2 threads are accessing two different methods say one is synchronized another is non-synchronized.

now thread one is entered synchronized method/block . and thread two can enter non-synchronized method simultaneously,
because to enter non-synchronized method you dont required object lock.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18763
    
  40

Baaziz Hamza wrote:In a given method we can synchronize a block of code on a third party object
exm :

I hope i understood this right : while a thread is executing the synchronized block of code, it 'll lock the OtherObject, & no other thread will be able to access that instance or call it's methods.


I was expecting that while the synchronized for of Toto is running, AppCertif.test instance will be locked, & Titi won't be able to access it or call it's method.
But I didn't get what I was expecting, instead during execution I get some [DELETED] which shows that both threads can access the AppCertif.test instance even if we're synchronizing on it !!!

I hope someone can explains to me, what I'm missing please !



Synchronization is cooperative. This....

while a thread is executing the synchronized block of code, it 'll lock the OtherObject, & no other thread will be able to access that instance or call it's methods.


is obviously incorrect, and probably better as...

while a thread is executing the synchronized block of code, it'll lock the OtherObject, & no other thread will be able to lock the same object.


If you other thread also tried to lock the object before accessing, then it would have blocked until the lock is freed.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Baaziz Hamza
Greenhorn

Joined: Aug 30, 2012
Posts: 8

Than you all for your answers
Henry
while a thread is executing the synchronized block of code, it'll lock the OtherObject, & no other thread will be able to lock the same object.

Can you explain to me what's your understanding of locking an instance please.
Because in my understanding, locking means preventing other threads from accessing that instance & executing it's methods, to prevent corrupted data that might happens when multiple threads are dealing with the same resource at the same time.

Regards
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 375
    
    1

Baaziz Hamza wrote:
Because in my understanding, locking means preventing other threads from accessing that instance & executing it's method
Regards


Well, your understanding is quite correct, but this doesn't fit with the requirement you are having..

while a thread is executing the synchronized block of code, it'll lock the OtherObject, & no other thread will be able to lock the same object.

This statement is an improvement over the one you gave..
Because, any other thread can still access the instance that has been locked in a thread, unless it wants to achieve a lock on it..

And you were saying saomething like this: -
while a thread is executing the synchronized block of code, it'll lock the OtherObject, & no other thread will be able to access the instance of OtherObject.


Compare both the statements and my reasoning..




R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 375
    
    1

And that was my 100th Post.
Baaziz Hamza
Greenhorn

Joined: Aug 30, 2012
Posts: 8

R. Jain wrote:And that was my 100th Post.

congrats
while a thread is executing the synchronized block of code, it'll lock the OtherObject, & no other thread will be able to access the instance of OtherObject.

I made an error in saying no other thread can access the locked instance, i was trying to say that no other thread can modify it. I edited it.
But i still don't get why, another thread can still call the synchronized instance methods, even if it's locked by another thread.
Or I have a confusion & is it allowed to call the methods, but not modifying the internal state ??
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

R. Jain wrote:And that was my 100th Post.

keep up!

@Baaziz Hamza : Welcome to JavaRanch
Baaziz Hamza
Greenhorn

Joined: Aug 30, 2012
Posts: 8

Seetharaman Venkatasamy wrote:@Baaziz Hamza : Welcome to JavaRanch

Thank you
Baaziz Hamza
Greenhorn

Joined: Aug 30, 2012
Posts: 8

Henry Wong wrote:If you other thread also tried to lock the object before accessing, then it would have blocked until the lock is freed.

Finally i found what i was doing wrong, (I guess my ping is kind of slow or I need to go through this chapter again ), what i should have done is also getting the same lock on the other thread that's accessing my synchronizing instance :

I was thinking that once we get a lock on an instance it'll automatically block other threads (even if they don't have the same lock).

Anyway thanks every one
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Baaziz Hamza wrote:Any ways thanks every one

You are Welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need some help, about synchronization topic