"Java Threads: Print Numbers in Parallel with 3 Threads"

Laura Jun 11, 2026

What is Printing Numbers using 3 Threads in Java and How to Do it Effectively?

Programming interviews often test a candidate's ability to tackle challenging problems through innovative coding approaches. One such problem involves printing numbers using three threads in Java, a task that showcases mastery over multi-threading and concurrency. In this article, we will delve into the world of multi-threading in Java, exploring the concept of using three threads to print numbers in a specific order.

How Multi-Threading Works in Java

Java offers a robust platform for developing multi-threaded applications, allowing for concurrent execution of multiple threads within a single program. This is achieved through the Thread class, which can be extended to implement custom thread behavior. There are two primary ways to create threads in Java: by extending the Thread class and implementing the Runnable interface.

80+ Pattern Programs In Java
80+ Pattern Programs In Java

Creating Threads in Java

Java threads can be created in two main ways:

  • Extending the Thread class: To create a new thread, a class must extend the Thread class and implement a run() method, which contains the code to be executed by the thread.
  • Implementing the Runnable interface: The Runnable interface requires implementing the run() method. A thread is then created by passing an instance of the implementing class to the Thread constructor.

Printing Numbers using 3 Threads in Java

To directly answer the question, the process of printing numbers using three threads involves creating and managing threads to ensure they are running concurrently and the numbers are printed in a specified order.

Step-by-Step Guide to Printing Numbers with Three Threads

Let's see how we can create threads that print numbers from 1 to 10, 11 to 20, and 21 to 30 concurrently.

58 Number Pattern Programs In Java | Pyramid and Diamond Pattern Programs
58 Number Pattern Programs In Java | Pyramid and Diamond Pattern Programs

  1. Define the ranges for each thread: Thread 1 will print numbers from 1 to 10, while Thread 2 will handle numbers 11 to 20, and Thread 3 will print numbers from 21 to 30.
  2. Create three separate classes for each thread, each implementing the Runnable interface or extending the Thread class, with the run() method containing a loop that prints numbers within its assigned range.
  3. In the main() method, create three instances of the Thread class, passing each instance to the constructor, and start all threads.
  4. Allow each thread to complete its execution using the join() method to ensure proper thread termination.

Ensuring Thread Safety

To avoid thread interference or interleaving of output, threads should operate on separate data structures or ranges. Here, since each thread is printing a separate range of numbers, no synchronization primitives are needed; however, if the output from each thread needed to be combined without any interleaving, a synchronized approach would be required.

Examples of Printing Numbers using 3 Threads

Below is an example demonstrating how to print numbers from 1 to 10, 11 to 20, and 21 to 30 according to the specifications.

class Thread1 implements Runnable {
    public void run() {
        for (int i = 1; i <= 10; i++) {
            System.out.print(i + " ");
        }
    }
}

class Thread2 implements Runnable {
    public void run() {
        for (int i = 11; i <= 20; i++) {
            System.out.print(i + " ");
        }
    }
}

class Thread3 implements Runnable {
    public void run() {
        for (int i = 21; i <= 30; i++) {
            System.out.print(i + " ");
        }
    }
}

public class Main {
    public static void main(String[] args) {
        Thread thread1 = new Thread(new Thread1());
        Thread thread2 = new Thread(new Thread2());
        Thread thread3 = new Thread(new Thread3());

thread1.start();
        thread2.start();
        thread3.start();

try {
            thread1.join();
            thread2.join();
            thread3.join();
        } catch (InterruptedException e) {
            // Handle the exception
        }
        System.out.println("
");
    }
}

Common Issues with Printing Numbers using 3 Threads

Despite the approach above seemingly efficient, printing numbers using multiple threads can sometimes lead to synchronization issues, especially if sharing resources are involved. Consider proper synchronization constructs, such as synchronized blocks, Lock objects, or even higher-level concurrent data structures to mitigate such issues.

Prime Number & Flowchart Notes for Beginners
Prime Number & Flowchart Notes for Beginners

Best Practices for Multi-Threading in Java

  1. Synchronization: Use synchronization when necessary to prevent data inconsistencies and reduce race conditions.
  2. Communication: Use invariants and shared memory for communication between threads when necessary, considering atomic data structures.

Frequently Asked Questions

How do threads in Java avoid interleaving output?

  • Threads are designed to run independently, and their output from shared printing statements is either contained within their own scope or properly synchronized for shared resources.

Can we use Java's ExecutorService for such a task?

  • Yes, the ExecutorService can facilitate thread management and synchronization, allowing for a more structured approach to concurrent programming.

Are there any use cases where using threads in Java is unnecessary?

  • Use threads for tasks that involve significant I/O operations, lengthy CPU-bound tasks, or activities requiring periodic execution scheduling.

Conclusion

Using multiple threads in Java can elegantly solve complex concurrency-related problems, offering a structured and efficient way to manage concurrent execution of tasks. Given the various methods for implementing and managing threads, understanding when and how to utilize this tool is crucial for tackling intricate programming challenges effectively.

Printing numbers using three threads offers a great example of utilizing multi-threading to achieve specific outcomes with precision and so creates a meaningful use case for those learning and practicing threading concepts. With a solid grasp of Java threading fundamentals, developers can extensively explore its capabilities across a range of applications.

Your turn is now to experiment and apply threading concepts in a variety of scenarios, further solidifying your understanding and skills in this powerful programming paradigm.

Java Programming CheatsheetIntro to Java Programming: An Interdisciplinary Approach and Computer Science: An Interdisciplinary Approach by Sedgewick and Wayne
Java Programming CheatsheetIntro to Java Programming: An Interdisciplinary Approach and Computer Science: An Interdisciplinary Approach by Sedgewick and Wayne
💥 MASTER JAVA THREADS! 💥
💥 MASTER JAVA THREADS! 💥
Inverted Pyramid number pattern in Java - Codeforcoding
Inverted Pyramid number pattern in Java - Codeforcoding
How to print in Java?
How to print in Java?
Java Data Types Explained
Java Data Types Explained
Number patterns in Java
Number patterns in Java
How To Create Pyramid Of Numbers In Java?
How To Create Pyramid Of Numbers In Java?
What is number class in Java?
What is number class in Java?
Example of Printing Java Array using the "for-each" Loop
Example of Printing Java Array using the "for-each" Loop
an image of a computer screen with some type of text on it's side
an image of a computer screen with some type of text on it's side
Palindrome Number in Java
Palindrome Number in Java
pyramid number pattern in Java using for loop - Codeforcoding
pyramid number pattern in Java using for loop - Codeforcoding
Insertion Sort Algorithm in Java with Example
Insertion Sort Algorithm in Java with Example
Deep Dive into java numbers-  Methods with syntax and examples
Deep Dive into java numbers- Methods with syntax and examples
Master Java Patterns
Master Java Patterns
1D Array in Java Explained | Java Array Basics for Beginners
1D Array in Java Explained | Java Array Basics for Beginners
Java Variables Explained | Local, Static & Instance Variables in Java
Java Variables Explained | Local, Static & Instance Variables in Java
maths
maths
30 Days of Java – Day 10 | Java Method Parameters Explained for Beginners
30 Days of Java – Day 10 | Java Method Parameters Explained for Beginners
Java Thread Life Cycle | Infographic
Java Thread Life Cycle | Infographic
the prime numbers poster is shown with instructions for each number, and two times to be added
the prime numbers poster is shown with instructions for each number, and two times to be added
4 Reasons and Benefits of Using Multithreading in Java? Why Threads?
4 Reasons and Benefits of Using Multithreading in Java? Why Threads?
an image of a program with numbers and words on the back ground, which reads c + + program to display prime numbers when larger number is entered first
an image of a program with numbers and words on the back ground, which reads c + + program to display prime numbers when larger number is entered first