jQuery in Action, 2nd edition*
The moose likes Threads and Synchronization and the fly likes difference between join and synchronisation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "difference between join and synchronisation" Watch "difference between join and synchronisation" New topic
Author

difference between join and synchronisation

pratibha pandey
Greenhorn

Joined: Mar 06, 2012
Posts: 1
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?
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1502
    
    5

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.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: difference between join and synchronisation
 
Similar Threads
inner join Vs outer join
synchronised(this)
left outer join Vs right outer join
JSPWriter difference write vs. print?
Synchronization question