-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberPrinting.java
More file actions
36 lines (31 loc) · 1005 Bytes
/
NumberPrinting.java
File metadata and controls
36 lines (31 loc) · 1005 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import java.io.*;
public class NumberPrinting {
public static void main(String[] args) {
Thread thread1 = new Thread(new NumberPrinter(1, 10));
Thread thread2 = new Thread(new NumberPrinter(90, 100));
thread1.start();
thread2.start();
}
static class NumberPrinter implements Runnable {
private int start;
private int end;
public NumberPrinter(int start, int end) {
this.start = start;
this.end = end;
}
public void run() {
printNumbers();
}
private void printNumbers() {
for (int i = start; i <= end; i++) {
System.out.println(Thread.currentThread().getName() + ":" + i);
try {
Thread.sleep(500);
// Adding delay for better visualization
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}