Question

In: Computer Science

Write an Application that extends the Thread class, call it MyThread.java. Have your new class accept...

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.

  1. 2.) Modify 1.) from above. Change this class to use the Runnable Interface instead of the Thread class. Any difference how the code runs?

  2. 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!

Solutions

Expert Solution

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:


Related Solutions

Write a class to accept a sentence from the user and prints a new word in...
Write a class to accept a sentence from the user and prints a new word in a terminal formed out of the third letter of each word. For example, the input line “Mangoes are delivered after midnight” would produce “neltd”. Program Style : Basic Java Programming
public class ProductThread { static class ProductThreads extends Thread{ private int begin, end; int[] v1, v2;...
public class ProductThread { static class ProductThreads extends Thread{ private int begin, end; int[] v1, v2; long ris; public ProductThreads(String name, int [] v1, int [] v2, int begin, int end) { setName(name); this.v1 = v1; this.v2 = v2; this.begin = begin; this.end = end; this.ris = 0; } public void run() { System.out.println("Thread " + Thread.currentThread().getName() + "[" + begin + "," + end + "] started"); ris = 1; for(int i = begin; i <= end; i++) ris...
Write a class that extends the LeggedMammal class from the previous laboratory exercise.
C++ code on Visual Studio Code:Write a class that extends the LeggedMammal class from the previous laboratory exercise. The class will represent a Dog. Consider the breed, size and is registered. Initialize all properties of the parent class in the new constructor. This time, promote the use of accessors and mutators for the new properties. Instantiate a Dog object in the main function and be able to set the values of the properties of the Dog object using the mutators....
Write a java program that has a class named Octagon that extends the class Circ and...
Write a java program that has a class named Octagon that extends the class Circ and implements Comparable (compare the object's area) and Cloneable interfaces. Assume that all the 8 sides of the octagon are of equal size. Your class Octagon, therefore, must represent an octagon inscribed into a circle of a given radius (inherited from Circle) and not introduce any new class variables. Provide constructors for clas Octagon with no parameters and with 1 parameter radius. Create a method...
Using your Barrel class and Temperature class create a VacuumBarrel class that extends the Barrel class...
Using your Barrel class and Temperature class create a VacuumBarrel class that extends the Barrel class and incorporates a Temperature as the private data of the VacuumBarrel. Provide constructors, add(VacuumBarrel), subtract(VacuumBarrel), divide(int), equals(VacuumBarrel), greaterThan(VacuumBarrel) (that has one parameter and returns true if the this is greater than the parameter), toKelvin(), toFahrenheit(), toCelsius(), and toString() methods. Use a similar demo to the one you used with the Temperature class. This demo will put instances of VacuumBarrel in three arrays and manipulates...
Instructions: Write a thread that discusses the situation. Your thread must consist of at least 3...
Instructions: Write a thread that discusses the situation. Your thread must consist of at least 3 paragraphs. You must summarize the key elements in the case. What legal statute(s) apply to the case? What issue(s) must the court decide in the case? If you were the judge, how would you rule? Did the employer discriminate unlawfully? Why or why not? The 6 major federal laws that are relevant are Title VII of the Civil Rights Acts of 1964 as amended...
In java Create a class named CellPhoneCase that extends your Case class – it inherits all...
In java Create a class named CellPhoneCase that extends your Case class – it inherits all the fields and methods from the Case class. It should also contain:  One field for a CellPhone object. Be sure to put in the appropriate access modifier. (private, public)  A constructor with 3 parameters – owner’s name, Case color, the phone number of the cell phone that will be in the Case. This constructor MUST call the super class’s constructor. Then set...
In C++ Write a class named TestScores. The class constructor should accept an array of test...
In C++ Write a class named TestScores. The class constructor should accept an array of test scores as its argument. The class should have a member function that returns the average of the test scores. If any test score in the array is negative or greater than 100, the class should throw an exception. Demonstrate the class in program.
Write a class named TestScores. The class constructor should accept an array of test scores as...
Write a class named TestScores. The class constructor should accept an array of test scores as its argument. The class should have a method that returns the average of the test scores. If any test score in the array is negative or greater than 100, the class should throw an IllegalArgumentException. Demonstrate the class in a program. Use TestScoresDemo.java to test your code public class TestScoresDemo { public static void main(String[] args) { // An array with test scores. //...
Write a public class call CountInts. class CountInts has a main method. CountInts must have two...
Write a public class call CountInts. class CountInts has a main method. CountInts must have two methods: count and displayResults. 1. method count takes an array, aa, of ints and counts how many times each integer appears in aa. The integers can only be from 0 to 100 inclusive. 2. method display results displays the results of the count 3. use the template provided. insert your code in the places indicated and don't change anything else. Examples % java CountInts...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT