Hello, I'm not sure if I understood the following task correctly.
Implement a PrintDigit class that implements a thread that prints a digit (i.e. a number between 0 and 9) on the screen n times in a row. Use the System.out.flush() method to ensure that any output appears on the screen immediately. The digit and the number n are input parameters
for the constructor and are stored as attributes.
-
Write a main program that spawns 10 threads and again waits for them to complete. Every thread should
output a different digit (0 ... 9), repeating the output 200 times.
public class PrintDigit implements Runnable {
private int digit;
private int n;
public PrintDigit(int digit, int n) {
this.digit = digit;
this.n = n;
}
public void run() {
for (int i = 0; i < n; i++) {
System.out.print(digit);
System.out.flush();
}
}
}
public class Main {
public static void main(String[] args) throws InterruptedException {
Thread[] threads = new Thread[10];
for (int i = 0; i < threads.length; i++) {
threads = new Thread(new PrintDigit(i, 200));
threads.start();
}
for (int i = 0; i < threads.length; i++) {
threads.join();
}
}
}
Implement a PrintDigit class that implements a thread that prints a digit (i.e. a number between 0 and 9) on the screen n times in a row. Use the System.out.flush() method to ensure that any output appears on the screen immediately. The digit and the number n are input parameters
for the constructor and are stored as attributes.
-
Write a main program that spawns 10 threads and again waits for them to complete. Every thread should
output a different digit (0 ... 9), repeating the output 200 times.
public class PrintDigit implements Runnable {
private int digit;
private int n;
public PrintDigit(int digit, int n) {
this.digit = digit;
this.n = n;
}
public void run() {
for (int i = 0; i < n; i++) {
System.out.print(digit);
System.out.flush();
}
}
}
public class Main {
public static void main(String[] args) throws InterruptedException {
Thread[] threads = new Thread[10];
for (int i = 0; i < threads.length; i++) {
threads = new Thread(new PrintDigit(i, 200));
threads.start();
}
for (int i = 0; i < threads.length; i++) {
threads.join();
}
}
}