• Post Reply Bookmark Topic Watch Topic
  • New Topic

Synchronized calling non-synchronized  RSS feed

 
Marcela Blei
Ranch Hand
Posts: 477
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you call a non-synchronized method within the body of a synchronized one, what happens with the locks?
I was trying code to find that the non-synchronized method can make unprecdictable results, but I can�t find an example.
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marcela,
Since the lock is held for the entire duration of the synchronized method, calling the non-synchronized method will not release the lock. However, there may be other threads which are executing the non-synch method concurrently.
The lock is released only when the synchronized method exits.

Ajith
 
Marcela Blei
Ranch Hand
Posts: 477
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ajith!
Another doubt:
Other threads can execute the non-synch method when it is executing called by the synch method?
Marcela
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure!. Non-synch method is the "plain vanilla" method. At any time more than one thread can execute this method. The calling thread from the synchronized method is just one such thread.
Ajith
 
Marcela Blei
Ranch Hand
Posts: 477
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I am very confused...
If the non-synch method is executing called by the synch method that locks the object (the synch method doesn�t release the lock because it calls the other method in the middle of its body), then while the non-synch is executing other threads can invoke it, so it is like the calling method (the synchronized one) releases the lock?
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marcela,
I am sorry I am not able to understand your question. Can you be more specific?
Ajith
 
deekasha gunwant
Ranch Hand
Posts: 396
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi marcela,
a lock is obtained for the SYNCHRONIZED methods only(lock is never never related to the non sync. methods).
so when a thread which is exceuting a synch. method
calls an async. method in between it still holds the lock on the object.which means that no other thread can execute any other
SYNCHRONIZED
method on that object. but this restriction is not applicable to non synch methods.
so while our thread which has the lock calls the non synch. method from the synch. method. it still holds the lock and won't let any other thread enter into a synch. method but for non synch. method it does not care( because a lock is obtained only on synch methods) .
hope i made some sense.
regards
deekasha
 
Marcela Blei
Ranch Hand
Posts: 477
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That make sense deekasha, you understand my confusion.
Thank you very much deekasha and Ajith.
I think that this topic should be move to the threads forum, because it�s not in the scope of the exam.
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am moving this topic to Threads forum.
Ajith
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!