In: Computer Science
Write an Application that extends the Thread class, call it
MyThread.java. Have your new class accept an integer(i.e. 200) when
it is instantiated. (MyThread mt = new MyThread(200); )
This integer number will be the number of times that this class
loops and prints a message. The message should read “Thread
Running...200”. The 200 would be whatever number is passed into the
constructor. If the number was 300, then the output should read “
Thread Running ..... 300”. Use a main method to test this class. In
the main start 2 Threads, one Thread with 250 and one Thread with
300. What happens? You do not need to use the sleep() method for
this exercise.
2.) Modify 1.) from above. Change this class to use the Runnable Interface instead of the Thread class. Any difference how the code runs?
3.) Lastly, have the main method start 4 different threads, all with different loop counts. Test the application, what happens?
PLEASE provide a screenshot or image of the code running!
1. MyThread.java
import java.util.Scanner;
public class MyThread extends Thread {
public MyThread(int n) {
int i;
for(i=0;i<n;i++){
System.out.println("Thread Running.."+n);
}
}
public static void main(String[] args) {
int n;
Scanner sc = new Scanner(System.in);
System.out.println("enter an integer");
n = sc.nextInt();
MyThread mt = new MyThread(n);
MyThread m1 = new MyThread(250);
MyThread m2 = new MyThread(300);
}
}
Output:
2. When we modify class to use runnable interface:
when we use runnable interface we need run() method to implement the interface:
import java.util.Scanner;
public class MyThread implements Runnable {
public MyThread(int n) {
int i;
for(i=0;i<n;i++){
System.out.println("Thread Running.."+n);
}
}
public static void main(String[] args) {
// TODO code application logic here
int n;
Scanner sc = new Scanner(System.in);
System.out.println("enter an integer");
n = sc.nextInt();
MyThread mt = new MyThread(n);
MyThread m1 = new MyThread(250);
MyThread m2 = new MyThread(300);
Thread m3 = new Thread(m1); //object for run() method
m3.start(); //starting thread m3
}
@Override
public void run() {
System.out.println("we need run method for implementing runnable
interface");
}
}
Output:
arguement set to 2,3 in place of 250,300 respectively for showing output
3. four threads in main:
import java.util.Scanner;
public class MyThread extends Thread {
public MyThread(int n,String a) {
int i;
for(i=0;i<n;i++){
System.out.println(a+"Thread Running.."+n);
}
}
public static void main(String[] args) {
int m,n,o,p;
Scanner sc = new Scanner(System.in);
System.out.println("enter first loop count");
m = sc.nextInt();
System.out.println("enter second loop count");
n = sc.nextInt();
System.out.println("enter third loop count");
sc.nextLine();
o = Integer.parseInt(sc.nextLine());
System.out.println("enter fourth loop count");
p = sc.nextInt();
MyThread mt = new MyThread(m,"First Loop count");
MyThread m1 = new MyThread(n,"Second Loop count");
MyThread m2 = new MyThread(o,"Third Loop count");
MyThread m3 = new MyThread(p,"Fourth Loop count");
}
}
Output: