Asynchronous Number Printing Using Threads
Date23/01/2026
Aim:To print numbers from 1 to 10 and 90 to 100 using two separate threads.
showing how multithreading works in Java.
Algorithm:
Step 1: Start the program
Step 2: Create a class called Number Printer with a method print numbers.
Step 3: Inside print number, use a for loop to print numbers from start to
end.
Step 4: Print the current thread name & numbers.
Step 5: In main(), create an object of number printer.
Step 6: Start both threads using start() method.
Step 7: End the program.
Coding :
public class ThreadExample {
public static void main(String[] args) {
NumberPrinter numberPrinter = new NumberPrinter();
Thread thread1 = new Thread(() -> {
[Link](1, 10);
});
Thread thread2 = new Thread(() -> {
[Link](90, 100);
});
[Link]();
[Link]();
class NumberPrinter {
public void printNumbers(int start, int end) {
try {
for (int i = start; i <= end; i++) {
[Link]([Link]().getName() + " : " + i);
[Link](500); // delay
} catch (InterruptedException e) {
[Link]();
Output :
Thread -1:90
Thread-0:1
Thread -0:2
Thread -1:91
Thread -0:3
Thread -1:92
Thread -0:4
Thread-1:93
Thread -0:5
Thread -1:94
Thread -0:6
Thread -1:95
Thread -0:7
Thread – 1:96
Thread – 0:8
Thread -1:97
Thread -0:9
Thread -1:98
Thread-0:10
Thread-1:99
Thread -1:100