Can we synchronize a run() method in Java?



Yes, we can synchronize a run() method in Java using the synchronized keyword before this method. If this method is synchronized, only one thread can execute this on a given instance of the object at any point in time. Here is a code snippet that shows how to define the run() method as synchronized:

@Override
synchronized run(){
   //code implementation
}

Here, the @Override annotation specifies that the run() method overrides a method from the Runnable interface, and synchronized is a reserved keyword in Java used to define a method or block as synchronized.

Synchronization of a run() method is not required, because this method has been executed by a single thread only. It is good practice to synchronize a non-static method of another class because it is invoked by multiple threads at the same time.

In Java, Synchronization is a mechanism used to control the access of multiple threads to any shared resource. When multiple threads access any shared resources, the synchronization prevents data corruption by coordinating access, avoiding race conditions (i.e., occurs when multiple threads concurrently access and modify shared data).

The run() Method

The run() method of the Thread class is used to define the sequence of actions that a thread will execute. This method will be invoked by the Java Virtual Machine (JVM) implicitly; we can also invoke this method explicitly in the current thread without calling the start() method.

Following is the syntax of the run() method:

public void run()

Example

In the following example, we override (using @Override annotation) the run() method of the Runnable interface and synchronize it using the synchronized keyword to make sure that only one Thread can execute it at a time:

public class SynchronizeRunMethodTest implements Runnable {

   //synchronized run() method
   @Override
   public synchronized void run() {
      System.out.println(Thread.currentThread().getName() + " is starting");
      for(int i=0; i < 5; i++) {
         try {
            Thread.sleep(1000);
            System.out.println(Thread.currentThread().getName() + " is running");
         } catch(InterruptedException ie) {
            ie.printStackTrace();
         }
      }
      System.out.println(Thread.currentThread().getName() + " is finished");
   }
   
   public static void main(String[] args) {
      SynchronizeRunMethodTest test = new SynchronizeRunMethodTest();
      Thread t1 = new Thread(test);
      Thread t2 = new Thread(test);
      t1.start();
      t2.start();
   }
}

Below is the output of the above program:

Thread-0 is starting
Thread-0 is running
Thread-0 is running
Thread-0 is running
Thread-0 is running
Thread-0 is running
Thread-0 is finished
Thread-1 is starting
Thread-1 is running
Thread-1 is running
Thread-1 is running
Thread-1 is running
Thread-1 is running
Thread-1 is finished
Updated on: 2025-08-28T14:46:52+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements