• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

calling another method from synchronised block

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class A{

public void foo(){
sync (this){
bar(); // calls bar() that is not sync
}
}

public void bar(){
// not syncronised.

}
}

Suppose i have a sync block in foo(). That in turn calls bar() which is not syncnorised.
Page 35 (Chapter 9)of Kathy Siera says -

When a thread is executing code from within a synchronized block, including any method code invoked from that synchronized block, the code is said to be executing in a synchronized context.


Now if any other thread wants to access this bar(), does it have to wait until any previous thread's lock is released ??
 
author
Posts: 23909
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Now if any other thread wants to access this bar(), does it have to wait until any previous thread's lock is released ??



No... when a synchronized method (or block) calls an non-synchronized method, it merely just keeps the lock (does not release the lock). It doesn't mean that the non-synchronized method requires the lock -- other threads can still call it directly without requiring the lock.

Henry
 
Ganesh.P Ram
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Henry,

As per my understanding also, the thread does not get the lock on the object which has the called method.
But the book seems to convey (pls read my quote) that, the thread has a lock on the other object.
That is what is confusing me !!!
 
Henry Wong
author
Posts: 23909
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ganesh.P Ram:
Hi Henry,

As per my understanding also, the thread does not get the lock on the object which has the called method.
But the book seems to convey (pls read my quote) that, the thread has a lock on the other object.
That is what is confusing me !!!



There is not enough information for me to fully know the context of the quote -- but I am assuming that the book is saying that the thread still have the lock when if calls the nonsynchronized method from a synchronized method... Meaning that it does not give up the lock.

Henry
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Ganesh,
your question is not in a good passion to read.Don't think all the peaple who are posting threads are perfectly have a K&B book.
So please follow the rules mensioned by the ranch creators as this threads read by all the people in the world. Please give all the details in question.(In your quesion, I think, What is the "sync" is)please follow the rules.
So while posting threads follow the rule " ASK MEANINGFUL QUESTION AND LET OTHERS GET WHAT DO YOU WANT"
reply
    Bookmark Topic Watch Topic
  • New Topic