PSYCHOCODES

Thread Synchronization Program in Java-Multithreading

Multithreading is the process of two or more tasks executing concurrently in a single program. Threads are independent small tasks executing concurrently in a single program. In Java, threads are managed by the Thread class present in the java.lang package.

In this tutorial, we will see how to execute threads in synchronisation (one by one) and we will also see how threads got executed without synchronisation.

Code for creating a thread in Java

class MyThread extends Thread{
  
  public void run(){

     //your  code will come here
    
    }
  }

Creating the Thread object and starting the thread.

/*
main class
*/
Thread t = new MyThread();
t.start()       //starting the thread

Thread Scheduler

Before writing the code you must understand the thread scheduler. Thread Scheduler is basically a program which decides which thread needs to be executed and for how much time and most importantly you can't control the Scheduler. Now let's see how the threads work without Synchronisation.

Multithreading without Synchronisation

class MyThread extends Thread{
  
  public void foo(){
    System.out.println("Thread 1 Running.");
  }
  
  public void run(){
    for(int i=0;i<5;i++){
      foo();
    }
  }
}
class MyThread2 extends Thread{
  
  public void foo(){
    System.out.println("Thread 2 Running.");
  }
  
  public void run(){
    for(int i=0;i<5;i++){
      foo();
    }
  }
  
}
class Main{
    public static void main(String[] args){
      
      Thread t1 = new MyThread();
      Thread t2 = new MyThread2();
      t1.start();
      t2.start();
        
    }
}

Now let's see the output of the above code. We have two outputs to show you of the same code for the sake of better understanding.

Output-1:

Thread Synchronization java

Output-2:

Thread Synchronization Java

As you can see in output-1 thread two got executed first and in output-2 thread one got executed first, so we can say that scheduler is deciding itself which thread needs to be executed first if you execute the code 2-3 times more you will get some random outputs.

Multithreading with Synchronisation

One of the best methods to help your threads take turns is to put them to sleep periodically, all you need is to call the static sleep() method.

try{
Thread.sleep(1000);
}
catch(InterruptedException e){
e.printStackTrace();
}

Now let's see the code for Thread Synchronisation.

import java.util.Scanner;
class MyThread extends Thread{
  
  public void foo(){
    System.out.println("Thread 1 Running.");
  }
  
  public void run(){

    for(int i=0;i<5;i++){
      foo();
    }
  }
}
class MyThread2 extends Thread{
  
  public void foo(){
    System.out.println("Thread 2 Running.");
  }
  
  public void run(){
    try{
      Thread.sleep(1000);
    }
    catch(InterruptedException e){
      e.printStackTrace;
    }
    for(int i=0;i<5;i++){
      foo();
    }
  }
  
}
class Main{
    public static void main(String[] args){
      
      Thread t1 = new MyThread();
      Thread t2 = new MyThread2();
      t1.start();
      t2.start();
        
    }
}

The output of the above code will be always same as output-2 (shown above).

Thank you for reading.

Tweet your queries and feedback to @PsychoCodes or leave a message on our Facebook page. You can also comment your questions below.

Also, don't forget to subscribe to our Newsletter.

If you like this article, then please share it and help us grow.

Share your thoughts