• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

synchronised code or block???

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,
i read in a book that sometimes synchronisation(method) even does
not solve the concurrency problem and we have to use a synchronised block or wait in a sync. block with a boolean variable...
what is this all about. kindly explain with a small example the differnce between a sync blosk and snyc code..
regards
srijan
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by srijan sharma:
hi all,
i read in a book that sometimes synchronisation(method) even does
not solve the concurrency problem and we have to use a synchronised block or wait in a sync. block with a boolean variable...
what is this all about. kindly explain with a small example the differnce between a sync blosk and snyc code..
regards
srijan


Hi, srijan
Could you give me the original statement? Actually, the sync. block is more flexible than sync. method because the former can put thread into different objects in the same method. Thread calling sync. method just can enter monitor of the object where the sync method exists.
I am not sure what your statement means, however, I actually hear about the famouse "philosopher problem" which said a question caused by sychronization method, Maybe that can help you to understand the hi-level sychronization. did u know that?
regards
George

[This message has been edited by George Toronto (edited January 30, 2001).]
 
Ranch Hand
Posts: 1871
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi srijan sharma,
Basically there are two ways one can obtain a lock on an object. The first is the "synchronized method" and another is the "synchronized block".
Part I
The synchronized method is declared for an instance or a static block as following
Example I

In the case of x & x1 method the lock on the instance is obtained.
In the case of y & y1 the lock on the class of temp is obtained.
What does this lock mean??
The lock on an instance method like x means that while a thread is in the x method no other thread can enter the x1 method too. Similar is the case for the y and the y1 methods.
The synchronized method in case of the instance locks the Object that is associated with "this". In the case of the static synchronized method a lock on the Class associated with temp is obtained.
Part II
There is another way to lock Objects. This is with the "synchronized Block"
The synchronized block is declared for an instance or a static block as following
Example II

The Examples I & II both undertake the same functionality. There may be some reasons why one can opt for the Example II model rather than the Example I model. This is to put all non synchronized code outside the method block and then use the synchronized block when it is really needed like this


However the synchronized method cannot be used when i want to lock an Object not associated with the current instance - "this" or the current class " temp.Class". In this scenario i need to use the synchronized block.
Example II

Hope things are clear. If anything is unclear do get back.
Regds
Rahul P. Mahindrakar
 
srijan sharma
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks rahul,
but i wanted a real multithreaded example where i can understand real importance of both synch. block and synch. methods..
or suggest some cool links ..
regards
srijan
 
Rahul Mahindrakar
Ranch Hand
Posts: 1871
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
check out a few resources here
 
George Toronto
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi, Rahul
It is a good statement for the basic concepts for sychronization, but srijan's question is not still solved.
Actually, sychronization solve the concurrency question, but it has two problems.
1) sychronizing each methods in a class is a bad idea, If it is done, the multithread machanism does not exist. Right? So sychronization should be applied in the appriciate situation.
2) the "philosopher problem" still exists in Java multithread mechanism and is also caused by the sychronization. the prolem can be descripted as the following: 5 philosapher is sitting around a tale and wanna eat their dinner, but there are only 5 chop sticks at the table(you know, if they wanna eat at the same time, there have to be 10 chop sticks at the table). So they have to share the chop sticks, that is, only 2 philosophers can eat together and others have to wait.( of coz, they are stupid enough not to know go to kichen and bring other 5 chop sticks ). That means the sychronization cannot slove the concurrent prolem.
That is just my undertanding. Plz correct them if I make mistakes.
rgds
George
[This message has been edited by George Toronto (edited January 30, 2001).
[This message has been edited by George Toronto (edited January 30, 2001).]
 
I don't always make ads but when I do they're tiny
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic