Simple Program to print numbers using threads
NumberPrinter.java
public class NumberPrinter implements Runnable{ int number; public NumberPrinter(int number){ this.number = number; } public void run(){ System.out.println("Printing Number from Thread "+ this.number); } }
Main.java
public class Main { public static void main(String[] args) { for (int idx=1;idx<=5;idx++){ Thread objthread = new Thread(new NumberPrinter(idx)); objthread.start(); } } }
Output
Printing Number from Thread 5 Printing Number from Thread 1 Printing Number from Thread 4 Printing Number from Thread 3 Printing Number from Thread 2