Advertisement

Google Ad Slot: content-top

Java Thread

~1 min read · Java
On this page

A thread in Java is a lightweight process that runs independently and concurrently with other threads. Java provides built-in support for multithreading, which helps improve performance by executing multiple tasks in parallel.

Creating a Thread in Java:

There are two main ways to create a thread in Java:

Method 1: Extending the Thread Class

  • start() starts a new thread and calls the run() method asynchronously.
  • sleep(500) makes the thread pause for 500ms.
class MyThread extends Thread { public void run() { // The code inside run() will execute in a separate thread for (int i = 1; i <= 5; i++) { System.out.println("Thread: " + i); try { Thread.sleep(500); // Pause for 500ms } catch (InterruptedException e) { e.printStackTrace(); } } } } public class Main { public static void main(String[] args) { MyThread t1 = new MyThread(); t1.start(); // Start the new thread } }
Try it yourself

Method 2: Implementing the Runnable Interface (Recommended):

class MyRunnable implements Runnable { public void run() { for (int i = 1; i <= 5; i++) { System.out.println("Runnable Thread: " + i); try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } } } } public class Main { public static void main(String[] args) { Thread t1 = new Thread(new MyRunnable()); t1.start(); // Start the thread } }
Try it yourself

Why use Runnable instead of Thread?

  • Java supports multiple inheritance via interfaces, not classes.
  • Implementing Runnable is more flexible (allows extending another class if needed).

Thread Methods:

Method

Description

start()

Starts a new thread and calls run() method.

run()

Defines the task to be executed in the thread.

sleep(ms)

Pauses the thread for the specified milliseconds.

join()

Waits for a thread to finish before continuing.

isAlive()

Checks if the thread is still running.

interrupt()

Interrupts a sleeping or waiting thread.


Example: Running Multiple Threads:

class MyThread extends Thread { public void run() { for (int i = 1; i <= 3; i++) { System.out.println(Thread.currentThread().getName() + " - Count: " + i); try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } } } } public class Main { public static void main(String[] args) { MyThread t1 = new MyThread(); MyThread t2 = new MyThread(); t1.start(); t2.start(); } }
Try it yourself

Thread Synchronization (Avoiding Race Conditions):

When multiple threads access a shared resource, race conditions may occur. We can prevent this using synchronized methods.

class SharedResource { synchronized void printNumbers(int n) { // Synchronized method for (int i = 1; i <= 3; i++) { System.out.println(Thread.currentThread().getName() + " - " + (n * i)); try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } } } } class MyThread extends Thread { SharedResource obj; MyThread(SharedResource obj) { this.obj = obj; } public void run() { obj.printNumbers(2); } } public class Main { public static void main(String[] args) { SharedResource obj = new SharedResource(); MyThread t1 = new MyThread(obj); MyThread t2 = new MyThread(obj); t1.start(); t2.start(); } }
Try it yourself