• 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

Concept of synchronized methods & code blocks

 
Ranch Hand
Posts: 2379
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ranchers,
I m about the following concept of multithreading. I think the concept is correct but help me with explaination/codes.
The Concept :
--------------
When locking an object through using synchronized methods we only lock the methods that r syncronized and other non-synchronized methods r as well unaccessible from other threads who want to work on the same object, until the current thread working on the object does not leave the lock of the object. If the methods r not synchronozed but only synchronized code blocks r used then the same thing is happenning,i.e. the threads have to wait for the lock.

------------------
azaman
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ashik,
You've got the concept right

In the above code, <code>readX()</code> is not synchronized. It can be invoked at any time and will return whatever value of 'x' it finds without having to wait to obtain a lock on 'x'.
The <code>writeX(int i)</code> method is <code>synchronized</code>; before a thread can actually execute a write of 'x' it must first obtain the lock. So writes have to wait their turn but reads don't.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Ashik Uzzaman
Ranch Hand
Posts: 2379
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thnx Jane! Would u see my other post related ti this one ...
------------------
azaman
 
Where all the women are strong, all the men are good looking and all the tiny ads are above average:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic