What is the Thread.run() method in Java?

Overview

The run() method in Java is used to start a new thread. The Java Virtual Machine calls the run() method of this thread. When a Java program starts running, one thread begins running immediately. This is usually the main thread of the program. The main thread creates other threads as needed, for example, by calling the new Thread(target). The other threads created by the main thread may end before the main thread ends. In that case, when the main thread exits, the Java Virtual Machine terminates.

The run() method of a thread is where the thread works. In addition to calling other methods, the run() method can also be called from other threads. When a new thread is created, the run() method is not called automatically. We must call the thread’s start() method to start the thread’s work. When a thread's start() method is called, the Java Virtual Machine calls the run() method of that thread.

Syntax

The following is the syntax of the run() method:

public void run();
the syntax of run() method

Parameters

This method does not take any parameters.

Return value

This method does not return a value.

Exception

This method does not throw any exception.

Example

This example will print the name of the current thread. If a new thread is created, it will run concurrently with the calling program and its output will be interleaved with the output from this example.

public class RunnableDemo implements Runnable {
private Thread t;
private String threadName;
RunnableDemo( String name) {
threadName = name;
System.out.println("Creating " + threadName);
}
public void run() {
System.out.println("Running " + threadName );
try {
for(int i = 4; i > 0; i--) {
System.out.println("Thread: " + threadName + ", " + i);
// Let the thread sleep for a while.
Thread.sleep(1000);
}
} catch (InterruptedException e) {
System.out.println("Thread " + threadName + " interrupted.");
}
System.out.println("Thread " + threadName + " exiting.");
}
public void start () {
System.out.println("Starting " + threadName );
if (t == null) {
t = new Thread (this, threadName);
t.start ();
}
}
}
class TestThread {
public static void main(String args[] ) {
RunnableDemo r1 = new RunnableDemo( "Thread-1");
r1.start();
RunnableDemo r2 = new RunnableDemo( "Thread-2");
r2.start();
}
}

Explanation

  • Line 1: In this code, there are two threads. The first one is called Thread-1 and the second one is called Thread-2. We use the class RunnableDemo to create them.
  • Line 10: The run() method in each thread prints some information about the thread. For example, it prints the thread's name and how many times it has run. Then it sleeps for a while. Sleeping allows other threads to run and do their work.
  • Lines 10–23: We declare the run() method to throw an InterruptedException. This exception is thrown when a thread is interrupted while it is sleeping or waiting for another thread.
  • Line 26–31: When the program starts, we call the start() method of each thread. This in turn calls run(). The output from the two threads is interleaved because they are running concurrently. We can also see that when a thread exits, its name is printed to show that it has completed its work.

Conclusion

This is a simple example of how threads can be used to run several parts of a program at the same time. Threads can also be used for other purposes, such as waiting for information from a network or from another part of the program.

Free Resources