Complete the following program. This program should do the following:
1. Creates a random integer in the range 10 to 15 for variable: allThreads, to create a number of threads.
2. Creates a random integer for the size of an ArrayList: size.
3. Each thread obtains a smallest number of a segment of the array. To give qual sized segment to each thread we make the size of the array divisible by the number of threads: while(size%allThreads != 0)size++
4. The program gives random integers to the ArrayList: a.
5. To make sure the smallest number of the entire array is the same of your output, I made a method named: sequentialSmallest(a)
6. The program creates a number of threads. Each thread obtains the smallest number of a segment.
7. You need to complete the method: run() below.
Note: If your answer depends to a variable location that two or more threads are writing to it (changing its value), you must synchronize the variable.
import java.util.*;
public class Main {
public static void main(String[] args) {
// This method is complete. Do not change it.
new Main();
}
public Main() {
Q1Threads();
}
private void Q1Threads(){
// This method is complete. Do not change it.
Random rand = new Random();
int allThreads = rand.nextInt(5) + 10;
int size = rand.nextInt(50) + 1;
while(size%allThreads != 0)size++;
ArrayList<Integer> a = new ArrayList<Integer>(size);
for(int i = 0; i < size; i++)
a.add(rand.nextInt(100) + 10);
sequentialSmallest(a);
MyThread[] thrds = new MyThread[allThreads];
int segment = size/allThreads;
for(int i = 0; i <allThreads ; i++) {
int firstIndex = i * segment;
int lastIndex = i * segment + segment -1;
thrds[i] = new MyThread(a, firstIndex, lastIndex);
}
for(int i = 0; i < allThreads; i++)
thrds[i].start();
try{
for(int i = 0; i < allThreads; i++)
thrds[i].join();
}catch(Exception e){
System.out.println("Error: " + e);
System.exit(0);
}
System.out.println("The smallest number is: " + Shared.result);
}
private static void sequentialSmallest(ArrayList<Integer> a) {
// This method is complete. Do not change it.
int smallest = a.get(0);
for(int i = 0; i < a.size(); i++)
if(a.get(i) < smallest)
smallest = a.get(i);
System.out.println("The list of random numbers is:");
for(int i = 0; i < a.size(); i++)
System.out.print(a.get(i) + ", ");
System.out.println("\nThe smallest number from the sequential list is: " + smallest);
}
}
class MyThread extends Thread{
private ArrayList<Integer> a;
private int from, too;
public MyThread(ArrayList<Integer> a, int from, int too) {
this.a = a;
this.from = from;
this.too = too;
}
public void run(){
// Complete this method
}
}
Answer:
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks
import java.util.*;
public class Main {
public static void main(String[] args) {
// This method is complete. Do not change it.
new Main();
}
public Main() {
Q1Threads();
}
private void Q1Threads() {
// This method is complete. Do not change it.
Random rand = new Random();
int allThreads = rand.nextInt(5) + 10;
int size = rand.nextInt(50) + 1;
while (size % allThreads != 0)
size++;
ArrayList<Integer> a = new ArrayList<Integer>(size);
for (int i = 0; i < size; i++)
a.add(rand.nextInt(100) + 10);
sequentialSmallest(a);
MyThread[] thrds = new MyThread[allThreads];
int segment = size / allThreads;
for (int i = 0; i < allThreads; i++) {
int firstIndex = i * segment;
int lastIndex = i * segment + segment - 1;
thrds[i] = new MyThread(a, firstIndex, lastIndex);
}
for (int i = 0; i < allThreads; i++)
thrds[i].start();
try {
for (int i = 0; i < allThreads; i++)
thrds[i].join();
} catch (Exception e) {
System.out.println("Error: " + e);
System.exit(0);
}
System.out.println("The smallest number is: " + Shared.result);
}
private static void sequentialSmallest(ArrayList<Integer> a) {
// This method is complete. Do not change it.
int smallest = a.get(0);
for (int i = 0; i < a.size(); i++)
if (a.get(i) < smallest)
smallest = a.get(i);
System.out.println("The list of random numbers is:");
for (int i = 0; i < a.size(); i++)
System.out.print(a.get(i) + ", ");
System.out
.println("\nThe smallest number from the sequential list is: "
+ smallest);
}
}
class MyThread extends Thread {
private ArrayList<Integer> a;
private int from, too;
public MyThread(ArrayList<Integer> a, int from, int too) {
this.a = a;
this.from = from;
this.too = too;
}
public void run() {
// assuming from<=too and both are valid indices on a
// taking element at from index as smallest
int smallest = a.get(from);
// looping through indices from+1 to too
for (int i = from + 1; i <= too; i++) {
// if element at index i is smaller than smallest, updating smallest
if (a.get(i) < smallest) {
smallest = a.get(i);
}
}
// now we need to update Shared.result if necessary. since this is a
// shared variable, we need to synchronize the access so that no two
// objects can modify its value at the same time.
synchronized (Shared.result) {
// if smallest is less than current Shared.result, updating
// Shared.result
if (smallest < Shared.result) {
Shared.result = smallest;
}
}
}
}
// a simple class to store the result
class Shared {
// initializing result to max value an int can hold, so that all other
// integers will be smaller than this value.
static Integer result = Integer.MAX_VALUE;
}
/*OUTPUT (random)*/
The list of random numbers is:
57, 87, 26, 96, 39, 83, 75, 80, 44, 68, 107, 89, 15, 108, 109, 15, 74, 55, 83, 11, 79, 106, 61, 43,
The smallest number from the sequential list is: 11
The smallest number is: 11
Get Answers For Free
Most questions answered within 1 hours.