• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

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
 
Why does your bag say "bombs"? The reason I ask is that my bag says "tiny ads" and it has stuff like this:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic