Difference between join method and synchronized method
Join method ensures synchronization of threads along with their sequence. Synchronized methods ensure synchronization of threads but not the sequence in which they run.
We have seen that thread synchronization can also be achieved by join() method. So, how is synchronized method is different from join method. And why is synchronized methods required at all if join() method is already there.
Below are the few differences
join method | synchronized method |
---|---|
Join method puts implicit lock in the thread object | Synchronized methods are used by putting a lock on the object they work upon |
join methods is used for sequencing of threads. E.g. if we call t1.join, then next thread will join after t1 completes | Synchronized methods are not used for sequencing. Rather they are used for synchronous access to an object |
Running a program that uses join method will make sure threads join in the order they are joined. | Synchronized methods ensure that threads run in a synchronous manner. They doesn’t ensure the sequence of threads |
In the below diagram also threads are accessing the synchronized method display one by one. But sequence is not gauranteed.