• 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

question on synchronizing

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi folks,
I'm kinda confused about the concept of synchronizing blocks/methods. Even though threads are for multitasking, don't they share the same processor? So how is it possible for threads to execute concurrently? And why we need synchronization if only one thread gets processed at a time? please correct me if i'm wrong. this concept is really confusing to me.
thanks in advance
regards
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well even though one thread is active in a single processor, see the following scenario:
consider a thread has some int variable , it reads it & then process it
one more thread which changes that variable & does some other stuff.
now, consider thread1 reads int value, then scheduler decides to run thread2, now thread 2 changes the value,then thread 1 starts again, but thread 1 has old value,
this is what we dont want, one thread using old value other using latest value. hence we got to lock those objects
hope this helps u
 
Ranch Hand
Posts: 289
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey CowBoy,
Your question So how is it possible for threads to execute concurrently? And why we need synchronization if only one thread gets processed at a time?
It appears that you are considering monitor(the object that contains synchronized code ) not to be distributed.That is you are considering that only CowBoy is running your app.But what if your application is distributed, so that Herbert,Vivek,Satya,Maha all have started VM's that are attempting to access your monitor ? Surely there is no central VM elsewhere that will ensure that my thread does not attemt to access your object at the same time that somebody else is. Do you see the potential disaster ? Do you see how concurrecy may happen ? Of course there are sevaral other ways.
Herbert
 
cowboy55
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you ppathak and herbert for your explanation. It helps me better to see what possible cases might happen that would require thread synchronization.
One question from ppathak's reply:
"hence we got to lock those objects"
what does it mean here to "lock those objects"? Can someone give more detailed explanation (if possible in relation to ppathak's scenario of one thread using the old changed variable?
thanks

 
Farmers know to never drive a tractor near a honey locust tree. But a tiny ad is okay:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic