How to Create Thread in Java: Simple Guide with Examples
In Java, you can create a thread by either extending the
Thread class or implementing the Runnable interface. Then, start the thread by calling the start() method, which runs the code in a new thread.Syntax
There are two main ways to create a thread in Java:
- Extending Thread class: Create a subclass of
Threadand override itsrun()method. - Implementing Runnable interface: Create a class that implements
Runnableand define therun()method, then pass an instance to aThreadobject.
Call start() on the Thread object to begin execution in a new thread.
java
class MyThread extends Thread { public void run() { // code to run in new thread } } class MyRunnable implements Runnable { public void run() { // code to run in new thread } } // To start thread: MyThread t1 = new MyThread(); t1.start(); Thread t2 = new Thread(new MyRunnable()); t2.start();
Example
This example shows creating two threads: one by extending Thread and another by implementing Runnable. Both print messages with a pause to simulate work.
java
class ThreadExample { public static void main(String[] args) { // Thread by extending Thread class class MyThread extends Thread { public void run() { for (int i = 1; i <= 3; i++) { System.out.println("MyThread running: " + i); try { Thread.sleep(500); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } } } // Thread by implementing Runnable class MyRunnable implements Runnable { public void run() { for (int i = 1; i <= 3; i++) { System.out.println("MyRunnable running: " + i); try { Thread.sleep(500); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } } } MyThread t1 = new MyThread(); Thread t2 = new Thread(new MyRunnable()); t1.start(); t2.start(); } }
Output
MyThread running: 1
MyRunnable running: 1
MyThread running: 2
MyRunnable running: 2
MyThread running: 3
MyRunnable running: 3
Common Pitfalls
Common mistakes when creating threads include:
- Calling
run()directly instead ofstart(). This runs code in the current thread, not a new one. - Not handling
InterruptedExceptionwhen usingsleep()or other blocking calls. - Sharing mutable data between threads without synchronization, causing unexpected behavior.
java
class WrongThread extends Thread { public void run() { System.out.println("Running in thread"); } } public class Test { public static void main(String[] args) { WrongThread t = new WrongThread(); t.run(); // WRONG: runs in main thread t.start(); // CORRECT: runs in new thread } }
Output
Running in thread
Running in thread
Quick Reference
Remember these key points when creating threads in Java:
- Use
start()to begin a new thread. - Override
run()to define thread work. - Implement
Runnablefor better flexibility. - Handle exceptions like
InterruptedException. - Synchronize shared data to avoid race conditions.
Key Takeaways
Create threads by extending Thread or implementing Runnable and override run() method.
Always call start() to run code in a new thread, not run() directly.
Handle InterruptedException when using sleep or blocking calls inside threads.
Use Runnable interface for better design and flexibility.
Synchronize shared data to prevent thread interference and data corruption.