Difference Between the start() and run() Methods in Java

In Java, multithreading is a powerful technique that allows developers to execute multiple tasks concurrently. To create and manage threads, Java provides the Thread class, which comes with two essential methods: start() and run(). While both methods are used for threading, they serve distinct purposes and have specific use cases. In this section, we will explore the key differences between the start() and run() methods in Java and understand when to use each of them.

start() Method

The start() method is a fundamental method in Java for creating and starting a new thread. When we invoke the start() method on a Thread object, it initiates a new thread of execution, and the code inside the run() method of that thread is executed concurrently with other threads in the program.

Here's how you typically use the start() method:

The start() method is responsible for:

  1. Allocating system resources for the new thread.
  2. Calling the run() method of the Thread object in a separate thread of execution.
  3. Managing the thread's lifecycle (e.g., starting, running, and terminating the thread).
  4. It is important to note that calling start() multiple times on the same Thread object will result in an IllegalThreadStateException. Each thread can only be started once.

Java run() Method

The run() method is another crucial method within the Thread class. Unlike the start() method, which is responsible for creating a new thread, the run() method defines the code that will be executed when the thread is started. We override the run() method to specify the task or job that the thread should perform.

Here's an example of overriding the run() method:

The run() method is the entry point for the code that runs within the thread. When we call start(), it initiates the thread and eventually invokes the run() method, executing the code specified within it.

Here is a complete Java code example that demonstrates the difference between the start() and run() methods in Java threads. We'll create two threads, one using start() and the other using run(), and observe the output to understand the distinction.

File Name: ThreadDemo.java

Output:

Thread 11: Count 1
Thread 11: Count 2
Thread 11: Count 3
Thread 11: Count 4
Thread 11: Count 5
Thread 1: Count 1
Thread 1: Count 2
Thread 1: Count 3
Thread 1: Count 4
Thread 1: Count 5

We define a ThreadDemo class that extends the Thread class and overrides the run() method to specify what the thread should do. In this case, it simply counts from 1 to 5 and prints the current thread ID and count.

In the main() method, we create two instances of ThreadDemo, thread1, and thread2.

We use the start() method to start thread1, and we use the run() method to execute thread2.

Explanation:

thread1 created using start() runs in a separate thread, and its run() method executes concurrently with the main thread (identified by Thread ID 11). The concurrent execution is evident from the interleaved output.

thread2 is created using run(), which simply executes the run() method in the context of the main thread. As a result, there is no concurrent execution, and the output from thread2 appears sequentially after thread1 has finished executing.

The output illustrates the key difference between the start() and run() methods in Java threads: start() creates a new thread for concurrent execution, while run() executes the code in the current thread sequentially.

Here's a tabular representation of the key differences between the start() and run() methods in Java threads:

Aspectstart() Methodrun() Method
Thread CreationInitiates a new thread and handles lifecycle.Does not create a new thread; runs in the current thread
ConcurrencyEnables concurrent execution of threads.Executes code sequentially in the current thread
Calling the MethodsCall to start a new threadOverride to define the thread's behavior
Execution BehaviorInitiates thread and invokes run() methodDefines the actual code that the thread should execute
Use CasesCreating and managing concurrent threadsDefining custom thread logic

The table summarizes the primary distinctions between the two methods, helping you understand when to use each of them in your Java threading applications.

Conclusion

In Java, understanding the difference between the start() and run() methods is crucial when working with multithreading. The start() method is used to initiate a new thread and is responsible for managing the thread's lifecycle, while the run() method defines the actual code that the thread should execute. By using these methods appropriately, we can harness the power of multithreading to create efficient and responsive Java applications.






Latest Courses