Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between Thread.join and Synchronized?

I am confused when to use Thread.join() and when to use synchronization in multi threading application.

According to me, both of them block or wait for the execution to be done by some other thread.
This example has to output 10 A's , 10 B's & 10 C's in sequential pattern one after other like :

1  : A
2  : A
3  : A
4  : A
5  : A
6  : A
7  : A
8  : A
9  : A
10 : A
1  : B
2  : B
3  : B
4  : B
5  : B
6  : B
7  : B
8  : B
9  : B
10 : B
1  : C
2  : C
3  : C
4  : C
5  : C
6  : C
7  : C
8  : C
9  : C
10 : C
----ProGraM ENDS----

Example starts here

class SyncTest extends Thread 
{   
    StringBuffer sb;

    public SyncTest(StringBuffer sb) 
    {
        this.sb = sb;   
    }

    public void run()
    {
        synchronized(sb) 
        {
            for(int i=1;i<=10;i++){
                System.out.println(i+" : "+sb.charAt(0));
            }
            sb.setCharAt(0, (char) (sb.charAt(0)+1));
        }
    }

    public static void main(String [] args) throws InterruptedException
    {
        StringBuffer sb = new StringBuffer("A");
        Thread t1=new SyncTest(sb);
        Thread t2=new SyncTest(sb);
        Thread t3=new SyncTest(sb);

        t1.start();

        t2.start();

        t3.start();

        Thread.sleep(1000);

        System.out.println("----ProGraM ENDS----");
    }
}

Here, output turns out to be 10 A's followed by 10 B's followed by 10 C's in a sequential order. But I can also use Thread.join instead of synchronized block to get the same output like this:

public void run()
    {
        //removed synchronized statement...

            for(int i=1;i<=10;i++){
                System.out.println(i+" : "+sb.charAt(0));
            }
            sb.setCharAt(0, (char) (sb.charAt(0)+1));

    }

    public static void main(String [] args) throws InterruptedException
    {
        StringBuffer sb = new StringBuffer("A");
        Thread t1=new SyncTest(sb);
        Thread t2=new SyncTest(sb);
        Thread t3=new SyncTest(sb);

        t1.start();
        t1.join();
        t2.start(); // wait for t1 to complete
        t2.join();
        t3.start(); // wait for t2 to complete
        t3.join(); 

                     // wait for t3 to complete
        System.out.println("----ProGraM ENDS----");
    }

Can anyone clear my confusion on usage of these 2 techniques i.e. when to use Thread.join and when to use synchronization in Multi-threading on Java.

like image 542
JPG Avatar asked Dec 02 '14 08:12

JPG


People also ask

What is difference between synchronized and thread safe?

Thread safe means that a method or class instance can be used by multiple threads at the same time without any problems occurring. Where as Synchronized means only one thread can operate at single time.

What does it mean to synchronize threads?

Synchronization is the cooperative act of two or more threads that ensures that each thread reaches a known point of operation in relationship to other threads before continuing. Attempting to share resources without correctly using synchronization is the most common cause of damage to application data.

What is the difference between synchronized and non synchronized?

Non synchronized -It is not-thread safe and can't be shared between many threads without proper synchronization code. While, Synchronized- It is thread-safe and can be shared with many threads.

What is the difference between volatile and synchronized?

Marking a variable as volatile basically tells all threads to do read and write operations on main memory only. synchronized tells every thread to go update their value from main memory when they enter the block, and flush the result back to main memory when they exit the block.


2 Answers

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.

like image 161
Duncan Jones Avatar answered Oct 13 '22 10:10

Duncan Jones


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.

like image 38
TheLostMind Avatar answered Oct 13 '22 08:10

TheLostMind