Implement something using multi-threading in Java
Published in
2 min readJun 12, 2024
Let’s implement a simple Java program that demonstrates multithreading. We’ll create a scenario where multiple threads print numbers concurrently. This example will illustrate the basic principles of creating, starting, and managing threads in Java.
Step-by-Step Implementation
- Create a class that implements
Runnable
interface: This class will define the task that each thread will execute. - Create and start multiple threads: We will create instances of the
Thread
class, passing ourRunnable
implementation to the constructor.
Complete Code Example
// NumberPrinter.java
public class NumberPrinter implements Runnable {
private String threadName;
private int start;
private int end;
public NumberPrinter(String threadName, int start, int end) {
this.threadName = threadName;
this.start = start;
this.end = end;
}
@Override
public void run() {
for (int i = start; i <= end; i++) {
System.out.println(threadName + ": " + i);
try {
// Sleep for a bit to simulate work and make the output more readable
Thread.sleep(100);
} catch (InterruptedException e) {
System.out.println(threadName + " interrupted.");
}
}
System.out.println(threadName + " finished.");
}
public static void main(String[] args) {
// Create instances of the Runnable task
NumberPrinter printer1 = new NumberPrinter("Thread-1", 1, 10);
NumberPrinter printer2 = new NumberPrinter("Thread-2", 11, 20);
NumberPrinter printer3 = new NumberPrinter("Thread-3", 21, 30);
// Create thread objects and pass the runnable tasks to them
Thread thread1 = new Thread(printer1);
Thread thread2 = new Thread(printer2);
Thread thread3 = new Thread(printer3);
// Start the threads
thread1.start();
thread2.start();
thread3.start();
// Wait for all threads to complete
try {
thread1.join();
thread2.join();
thread3.join();
} catch (InterruptedException e) {
System.out.println("Main thread interrupted.");
}
System.out.println("All threads finished.");
}
}
Explanation
NumberPrinter Class:
- Implements the
Runnable
interface, allowing instances to be executed by a thread. - The
run
method defines the code that constitutes the task of the thread. It prints numbers fromstart
toend
, prefixed by the thread's name. - The constructor takes parameters for the thread name, start, and end numbers.
Main Method:
- Three instances of
NumberPrinter
are created, each configured to print a different range of numbers. - Three
Thread
objects are created, each initialized with one of theNumberPrinter
instances. - The
start
method is called on each thread to begin execution. - The
join
method is used to wait for all threads to complete before the main thread proceeds.
Running the Program
Compile and run the program using the following commands in your terminal or command prompt:
javac NumberPrinter.java
java NumberPrinter
You should see the output from each thread interleaved, demonstrating concurrent execution. Note that the exact order of output may vary each time you run the program, due to the nature of thread scheduling.