This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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.