What is the difference between Thread.join and Synchronized?

余生长醉 提交于 2019-12-03 05:57:32

Thread.join() waits for the thread to completely finish, whereas a synchronized block can be used to prevent two threads from executing the same piece of code at the same time.

It's hard to advise when to use one over the other in general, since they serve different purposes. It's rare to find an example, such as your code, where the difference between the two is minimal.

That being said, in your first example there is no guarantee the output will be alphabetical. You can't be sure which thread will get to the synchronized block first. So in this particular case, join() is most appropriate.

thread.join() stops the execution of current thread until the joined thread completes.. You have commented correctly.. :)

Synchronization prevents multiple threads from executing the synchronized part of code on the same instance.

The synchronized keyword enables a locking mechanism that allows threads to not step on each other. Java documentation describes this as a way to " preventing thread interference and memory consistency errors".

If you use join(), it makes sure that as soon as a thread calls join,the current thread(running thread) will not execute unless the thread you have called join is finished. I think the diagram below might help visualize this better.

Source

Without join() thread runs in parallel and depend upon OS time slice (which to start first). With join() thread runs in series. For eg: Suppose you have two threads with both calling join() method

MyThread t1 = new MyThread();
MyThread t2 = new MyThread();

t1.join(); // this will be start first.
t2.join(); // this will be start only after above.

Now without join() method, any of t1 and t2 can start first. There's is no guarantee.

synchronized statement/keyword is used for monitoring the thread, so that only one thread can access that synchronized method/variable at a time. It doesn't matter if you use join() or not.

If you use synchronized with join(), you have a guarantee that thread t1 can only access first. Without synchronized both t1 and t2 thread can access at any time but these threads start and die. in series because of join().

stdout

They are obviously NOT the same but, if they are going to be used for the same purpose (serializing access/execution ) then synchronized blocks can be thought as a more flexible version than using joins since it's usage is agnostic about particular thread instances on which you want to serialize execution.

Moreover, in synchronized blocks the shared data block concept is more emphasized than joins.

Simple example :

You have a static string table where some threads will be putting some values . The table is initialized with "empty" . the table is accessed with a static index. If a thread puts a value, it will increment the static index .

If you synchronize the threads it will make sure that any value put by a thread can not be overriden by another thread .

if you use join on threads, only the first joinded thread will have the opportunity to put the values in the table .As the other will be waiting but knowing that the index is incremented they wont be able to access the table (null pointer exception) . So Join has made the other thread useless.

This example uses threads on same instance containing the synchronized method.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!