I need to write an Application that extends the Thread class. My class accepts an integer(i.e. 100) when it is instantiated. (MyThread myt = new MyThread(100); )
This integer number will be the number of times that this class loops and prints a message. The message should read “The Thread is running… 100”. The 100 would be whatever number I passed into the constructor. If the number was 150, then the output should read “ The Thread is running… 100”. I should use a main method to test this class. In the main I will start 2 Threads, one Thread with 150 and one Thread with 200. I don't need to use the sleep() method for this code.
I already wrote a code, but I'm confused. Should my message be printed 100 times? I'm not sure if my code meets all requisites. I should also implement this code changing this class to use the Runnable Interface instead of Thread class
public class MyThread extends Thread {
private int numtimes;
public MyThread(int numtimes) {
this.numbtimes = numbtimes;
}
public void run() {
for (int i = 0; i < numbtimes; i++) {
System.out.println("Thread Running..." + numbtimes);
}
}
public static void main(String[] args) {
MyThread mytr1 = new MyThread(150);
mytr1.start();
MyThread mytr2 = new MyThread(200);
mytr2.start();
}
}
Is that what was asked? How would you do using Runnable Interface?