Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need some help, about synchronization topic

 
Baaziz Hamza
Greenhorn
Posts: 8
Google Web Toolkit Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(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
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20989
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Baaziz Hamza
Greenhorn
Posts: 8
Google Web Toolkit Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And that was my 100th Post.
 
Baaziz Hamza
Greenhorn
Posts: 8
Google Web Toolkit Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
R. Jain wrote:And that was my 100th Post.

keep up!

@Baaziz Hamza : Welcome to JavaRanch
 
Baaziz Hamza
Greenhorn
Posts: 8
Google Web Toolkit Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seetharaman Venkatasamy wrote:@Baaziz Hamza : Welcome to JavaRanch

Thank you
 
Baaziz Hamza
Greenhorn
Posts: 8
Google Web Toolkit Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Baaziz Hamza wrote:Any ways thanks every one

You are Welcome
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic