Multithreading

How to Print Numbers Using Multithreading in Java

Spread the love
Printing Numbers Without Threads
public class SequentialNumber {
    public static void main(String[] args) {
        // Printing numbers from 1 to 10 sequentially
        for (int i = 1; i <= 10; i++) {
            System.out.println("Number: " + i);
        }
    }
}
Explanation:
  1. Sequential Execution:
    • The program runs in the main() method.
    • It uses a simple loop (for loop) to print numbers from 1 to 10 in order.
  2. Single Thread (Main Thread):
    • This program uses only the main thread, which is the default thread running the application.
    • All the tasks are executed sequentially without any parallelism.
Printing Numbers With Threads
// Creating a thread to print numbers
class NumberThread extends Thread {
    @Override
    public void run() {
        for (int i = 1; i <= 10; i++) {
            System.out.println(Thread.currentThread().getName() + " prints: " + i);
            try {
                Thread.sleep(500); // Adding a small delay for readability
            } catch (InterruptedException e) {
                System.out.println("Thread interrupted: " + e.getMessage());
            }
        }
    }
}

public class MultithreadingNumberPrinter {
    public static void main(String[] args) {
        // Creating and starting two threads
        NumberThread thread1 = new NumberThread();
        NumberThread thread2 = new NumberThread();

        thread1.setName("Thread-1");
        thread2.setName("Thread-2");

        thread1.start(); // Start thread1
        thread2.start(); // Start thread2
    }
}
Explanation:
  1. Thread Creation:
    • The NumberThread class extends Thread and overrides the run() method.
    • Inside run(), it uses a loop to print numbers from 1 to 10.
  2. Concurrent Execution:
    • Two separate threads (thread1 and thread2) are created and started using the start() method.
    • Both threads run concurrently, printing numbers simultaneously.
  3. Delay:
    • Thread.sleep(500) adds a small delay between the numbers, helping you observe the multithreaded behavior more clearly.

Printing Numbers With Runnable Interface

class NumberRunnable implements Runnable {
    @Override
    public void run() {
        // Printing numbers from 1 to 10
        for (int i = 1; i <= 10; i++) {
            System.out.println(Thread.currentThread().getName() + " prints: " + i);
            try {
                Thread.sleep(500); // Adding a delay for readability
            } catch (InterruptedException e) {
                System.out.println("Thread interrupted: " + e.getMessage());
            }
        }
    }
}

public class RunnableExample {
    public static void main(String[] args) {
        // Creating Runnable instances
        NumberRunnable task = new NumberRunnable();

        // Creating threads and associating them with the Runnable task
        Thread thread1 = new Thread(task, "Thread-1");
        Thread thread2 = new Thread(task, "Thread-2");

        // Starting the threads
        thread1.start();
        thread2.start();
    }
}
Explanation:
    • The run() method contains the logic to print numbers from 1 to 10.
    • Threads (thread1 and thread2) are associated with the same NumberRunnable instance in this example, meaning they share the same task.
    • The Thread.sleep(500) adds a 500-millisecond delay, making it easier to observe the execution of the threads.

    Leave a Reply

    Your email address will not be published. Required fields are marked *