• 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

difference between join and synchronisation

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is the difference between join and synchronisation ? Both ensure that only one thread gets access to the code. Then how we'll decide when to use join and when to use synchgronisation?
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

pratibha pandey wrote:what is the difference between join and synchronisation ? Both ensure that only one thread gets access to the code. Then how we'll decide when to use join and when to use synchgronisation?


Welcome to CodeRanch!

The difference is pretty trivial. Yes, you are right about synchronization : it ensures(if used properly of course) that only one thread access the code. But join doesn't do this. Instead, join method waits till the thread dies.

So, we can see the basic difference that join is to be used only when you need to ensure that the thread dies and you need to do something after that. E.g. multiple threads are creating some files, and once all desired files are created, then code is expected to do something - in this scenario, using join makes sense.

But the magic of synchronization is in inter-thread-communication. E.g. consider producer-consumer model. Producer produces some objects, and put those in a queue. Consumer consumes the object. If queue is empty, consumer waits till an object gets there while producer puts the object and notifies (wakes up) the waiting threads. Here, it is pretty logical that one has to use synchronization.

Basically, those two are not similar at all, but very different things.

I hope this helps.
 
reply
    Bookmark Topic Watch Topic
  • New Topic