Question: All parts need to be completed in java program Part 1 To start, copy the following Java program into a .java file named Main.java. public

All parts need to be completed in java program

Part 1

To start, copy the following Java program into a .java file named Main.java.

public class Main

{

public static void main(String args[])

{

int target = (int) (Math.random() * 1000);

System.out.println("The number is " + target);

}

}

Add a class named FindItThread that extends the Thread class.

Add a constructor that accepts three parameters: the number to search for, the number where the search should begin, and the number where the search should end. Store these parameters in private variables.

Add a run method that searches for the number. This method should also use a for loop to check each value in the specified range to determine if it matches the target value. If a match is made, the thread should display a message like the second like shown above and terminate by exiting from the for loop.

In the Main class, add code to the main method to create and start the four threads. The threads should check the following ranges: Thread-0, 0-249; Thread-1, 250-499; Thread-2, 500-749; and Thread-3, 750-999.

Test the application two or more times to be sure it works correctly.

Part 2

To start, copy the same Java program above into a .java file named Main.java in a different folder than used for Part 1.

Add a class named Finder that implements the Runnable interface. The class should work like FindItThread class created in Part 1. In fact, you can copy in most of the code you wrote for steps 3 and 4 of Part 1.

In the Main class, add code to the main method to create and start the four Finder threads. Each should check the same ranges as the four threads in Part 1.

Test the application two or more times to be sure it works correctly.

Modify the Finder class so its run method uses the sleep method to cause the thread to sleep for 1 millisecond every ten times through the loop. If an InterruptedException occurs, display the exception at the console.

Test the application to be sure it still works correctly.

Part 3 10 points

In this part, you will create an application that's similar to the one that you coded for Part 2. However, in this exercise you'll include code so the thread that finds the number notifies a Monitor thread, which then interrupts all of the Finder threads. When a Finder thread is interrupted, it should display a line indicating that it has been interrupted and then end.

The resulting example output should look like the following:

The number is 20

Target number 20 found by Thread-1

Thread-2 interrupted

Thread-3 interrupted

Thread-4 interrupted

To start, copy the Java classes from Part 2 into a new folder for Part 3.

Add a class named Monitor to the project. This class should define a thread by extending the Thread class. Then, code a method named addThread that adds a thread to a private array list of Thread objects.

In the Monitor class, add a synchronized method named foundNumber that interrupts each thread in the threads collection, i.e., the array list mentioned above. This method should also set a boolean instance variable to true to indicate that the number has been found.

In the Monitor class, add a run method that tests the boolean variable within an infinite loop.

In the Main class, modify the main method so it creates and starts the Monitor thread, passes a reference to the Monitor thread to the Finder threads, and adds the four Finder threads to the Monitor thread.

In the Finder class, modify its constructor so it accepts a reference to the Monitor thread.

Modify the run method so it calls the Monitor thread's foundNumber method if it finds the target number. Also, modify this method so a message is displayed when the thread is interrupted.

Test the application to be sure that it works correctly. If necessary, stop the Monitor thread after the number is found.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!