Java Multithreading Approaches: Extending Thread Class vs Implementing Runnable Interface

Understanding Multithreading in Java 🔹 In this example, I explored two approaches for creating threads: • Extending the Thread class • Implementing the Runnable interface package multithreading; /* 1️⃣ Extending Thread Class When a class extends the Thread class, it becomes a thread class. Objects of this class can run in a separate thread. However, the actual task that the thread executes must be written inside the run() method. When start() is called, the JVM creates a new thread and internally invokes the run() method. */ class CommonResource extends Thread {   public void commonResource(String t){     System.out.println("common resources " + t);   }   @Override   synchronized public void run() {     String currT = Thread.currentThread().getName();     if(currT.equals("bheem")){       System.out.println("bheem thread");     }     else if(currT.equals("kalia")){       System.out.println("kalia thread");     }     else{       System.out.println("raju thread");     }     try{       Thread.sleep(5000);     }     catch(Exception e){       e.printStackTrace();     }     commonResource(currT);   } } /* 2️⃣ Creating a Common Resource using the Runnable interface. Instead of extending Thread, it is generally recommended to implement the Runnable interface. Advantages: • The task is separated from the thread. • The class can still extend another class (Java doesn't support multiple inheritance). • This approach is widely used in industry (especially with thread pools). */ class CommonResource1 implements Runnable {   public void commonResource(String t){     System.out.println("common resources " + t);   }   /*   Multiple threads will share the same object of this class.   Because the run() method is synchronized, only one thread   can execute it at a time for this shared object.   */   @Override   synchronized public void run() {     String currT = Thread.currentThread().getName();     if(currT.equals("bheem")){       System.out.println("bheem thread");     }     else if(currT.equals("kalia")){       System.out.println("kalia thread");     }     else{       System.out.println("raju thread");     }     try{       Thread.sleep(5000);     }     catch(Exception e){       e.printStackTrace();     }     commonResource(currT);   } } public class ThreadsUsesCommonResource {   public static void main(String[] args){     // Creating a shared resource object     CommonResource1 commonResource1 = new CommonResource1();     // Creating multiple threads that use the same resource     Thread t1 = new Thread(commonResource1);     t1.setName("kalia");     Thread t2 = new Thread(commonResource1);     t2.setName("bheem");     Thread t3 = new Thread(commonResource1);     t3.setName("raju");     // start() → JVM creates a new thread → run() method executes     t1.start();     t2.start();     t3.start();   } } #Java #Multithreading #Synchronized #BackendDevelopment #LearningInPublic

To view or add a comment, sign in

Explore content categories