Question

THE SOLUTION MUST BE SUBMITTED AS 3 JAVA FILESConsider the threads hierarchy below: T2 T1 T3 T1 will ask the user to enter an array of size N and will fill the array with

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Main.java

package t1;

import java.util.Scanner; //for taking inputs
import java.util.concurrent.ExecutorService; //for making thread pools
import java.util.concurrent.Executors;
import java.util.concurrent.Future; //to call the threads


public class Main {

public static void main(String args[]) throws Exception {
   Scanner s = new Scanner(System.in);
   int n;
   int[] array = new int[100];
   System.out.print("\nEnter the number of grades: ");
   n = s.nextInt(); //taking input for number of grades
   System.out.print("\nEnter the grades: ");
   for(int i = 0; i<n ; i++) { //taking input for grades
       array[i] = s.nextInt();
   }
   ExecutorService service = Executors.newFixedThreadPool(1); //creating a pool of threads
   Future<Integer> m = service.submit(new MainThread(array, n)); //calling t1 thread calls constructor then call method
   service.shutdown(); //shutting down Executor Service
}
}

//MainThread.java

package t1;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;


public class MainThread implements Callable<Integer>{
   int[] array;
   int n;
public MainThread(int[] array,int n) {
   this.array= array; //we set the array in constructor
   this.n = n; //set the number of grades
}
public void print() { //print function to see contents of array
   System.out.println("\nIn MainThread");
   for(int i = 0; i<n ; i++) {
       System.out.print("\t"+array[i]);
   }
  
}
@Override
public Integer call() throws Exception{
   ExecutorService service = Executors.newFixedThreadPool(2); //creating a pool of 2 threads
   Future<Integer> max_pass = service.submit(new t2Thread(array, n)); //sending to t2 thread
   Future<Integer> max_fail = service.submit(new t3Thread(array, n)); //sending to t3 thread
   System.out.print("\n\nMax grade of Passed students: "+max_pass.get()); //value is received from t2.call() function
   System.out.print("\nMax grade of Failed students: "+max_fail.get()); //value is received from t3.call() function
   service.shutdown();
   return 0;
}
}

//t2Thread.java

package t1;
import java.util.concurrent.Callable;
public class t2Thread implements Callable<Integer>{
   int[] array = new int[100];
   int n;
  
   t2Thread(int[] array,int n) { //constructor to assign values to array and n
       this.array = array;
       this.n = n;
      
   }
   public void print() { //print function to see the contents of the array
   System.out.println("\nIn t2");
   for(int i = 0; i<n ; i++) {
       System.out.print("\t"+array[i]);
   }
  
}
   @Override
   public Integer call() throws Exception {
       int max_pass = -1;
       //print();
       for(int i = 0; i< n; i++) { //finding out the max grade of passed student
           if(max_pass < array[i] && array[i] >= 60) {
               max_pass = array[i];
           }
       }
       return max_pass; //returning the value
   }
      
}

//t3Thread.java

package t1;
import java.util.concurrent.Callable;

public class t3Thread implements Callable<Integer>{
   int[] array = new int[100];
   int n;
  
   t3Thread(int[] array,int n) {//constructor to assign values to array and n
       this.array = array;
       this.n = n;
      
   }
   public void print() { //print function to see the contents of the array
   System.out.println("\nIn t3");
   for(int i = 0; i<n ; i++) {
       System.out.print("\t"+array[i]);
   }
  
}
   @Override
   public Integer call() throws Exception {
       int max_fail = -1;
       //print();
       for(int i = 0; i< n; i++) { //finding out the max grade of failed student
           if(max_fail < array[i] && array[i] < 60) {
               max_fail = array[i];
           }
       }
       return max_fail;
   }

}


Main.java

*MainThread.java t2Thread.java Main.java X t3Thread.java 1 package t1; 2 30 import java.util.Scanner; //for taking inputs 4 i

MainThread.java*MainThread.java X t2Thread.java Main.java t3Thread.java 1 package t1; 2 3- import java.util.concurrent.Callable; 4 import ja

t2Thread.javaint n; ها . *MainThread.java t2Thread.java * Main.java t3Thread.java 1 package t1; 2 import java.util.concurrent.Callable; 3

t3Thread.javat3Thread.java X *MainThread.java t2Thread.java Main.java 1 package t1; 2 import java.util.concurrent.Callable; 3 9 } 4 public

OUTPUT
t2Thread.java Main.java Dt3Thread.java * *MainThread.java 1 package t1; Problems @ Javadoc Declaration Console X <terminated>

Add a comment
Know the answer?
Add Answer to:
THE SOLUTION MUST BE SUBMITTED AS 3 JAVA FILES Consider the threads hierarchy below: T2 T1...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • CSC1465L: Operating Systems Lab Consider the threads hierarchy below: T2 T1 T3 T1 will ask the...

    CSC1465L: Operating Systems Lab Consider the threads hierarchy below: T2 T1 T3 T1 will ask the user to enter an array of size N and will fill the array with student grades. T1 will then send the array to threads T2 and T3. T2 will compute the maximum grade of passed students. T3 will compute the maximum grade of failed students. T1 will print the maximum and minimum grades returned by threads T2 and 13. Note: A student passes an...

  • CSC1465L: Operating Systems Lab Consider the threads hierarchy below: T2 T1 T3 T1 will ask the...

    CSC1465L: Operating Systems Lab Consider the threads hierarchy below: T2 T1 T3 T1 will ask the user to enter an array of size N and will fill the array with student grades. T1 will then send the array to threads T2 and T3. T2 will compute the maximum grade of passed students. T3 will compute the maximum grade of failed students. T1 will print the maximum and minimum grades returned by threads T2 and 13. Note: A student passes an...

  • Need code written for a java eclipse program that will follow the skeleton code. Exams and...

    Need code written for a java eclipse program that will follow the skeleton code. Exams and assignments are weighted You will design a Java grade calculator for this assignment. A user should be able to calculate her/his letter grade in COMS/MIS 207 by inputting their scores obtained on worksheets, assignments and exams into the program. A skeleton code named GradeCompute.java containing the main method and stubs for a few other methods, is provided to you. You must not modify/make changes...

  • CS 2050 – Programming Project # 1 Due Date: Session 3 (that is, in one week!)....

    CS 2050 – Programming Project # 1 Due Date: Session 3 (that is, in one week!). Can be resubmitted up to two times until Session 6 if your first version is submitted by session 3. In this project, you create two classes for tracking students. One class is the Student class that holds student data; the other is the GradeItem class that holds data about grades that students earned in various courses. Note: the next three projects build on this...

  • Principles of Computer Science

    Question First, you need to design, code in Java, test and document a base class, Student. The Student class will have the following information, and all of these should be defined as Private: A. Title of the student (eg Mr, Miss, Ms, Mrs etc) B. A first name (given name) C. A last name (family name/surname) D. Student number (ID) – an integer number (of type long) E. A date of birth (in day/month/year format – three ints) - (Do NOT use the Date class from...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT