Question

kindly make a java threaded program main class will accept n number and produce user inputed...

kindly make a java threaded program

main class will accept n number and produce user inputed array of n elements

make 3 child threads(class) out of one main thread(class)

must use thread.sleep and thread synchronization

one thread create max of array elements

one will min of array elements

one will provide average of array elements

then display of array will occur

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

// Source Code:

import java.util.Scanner;

public class ThreadMainClass {
    public static void main(String[] args) {

        /*taking as input the array size and the array elements*/
        System.out.print("Enter size of array: ");
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        System.out.print("Enter array elements: ");
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = scanner.nextInt();
        }

        // creating the first child thread, it will compute the maximum in array 
        Thread child1 = new Thread(new Runnable() {
            @Override
            public void run() {
                // using synchronized for thread synchronization 
                synchronized (arr){
                    int maxEle = arr[0];
                    for (int i = 1 ; i < n; i++){
                        if(arr[i] > maxEle){
                            maxEle = arr[i];
                        }
                    }
                    System.out.println("Max Element : " + maxEle);
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            }
        });

        // creating the second child thread, it will compute the minimum in array
        Thread child2 = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (arr){
                    int minEle = arr[0];
                    for (int i = 1 ; i < n; i++){
                        if(arr[i] < minEle){
                            minEle = arr[i];
                        }
                    }
                    System.out.println("Min Element : " + minEle);
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });

        // creating the third child thread, it will compute the average of array
        Thread child3 = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (arr){

                    int sum = 0;
                    for (int i = 0 ; i < n; i++){
                        sum += arr[i];
                    }
                    int avg = sum/n;
                    System.out.println("Average : " + avg);
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });

        child1.start();
        child2.start();
        child3.start();


        try {
            // let the main thread wait for the other three child threads to complete their task
            Thread.currentThread().join(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // displaying the array
        for(int i=0;i<n;i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();
    }

}
// Output Screenshot:

Enter size of array: 4 Enter array elements: 24 5 6 Max Element : 6 Average : 4 Min Element : 2 2 4 5 6

// If you have any query do ak in the comments section

// If you found the answer helpful do give a THUMBS UP!!

Add a comment
Know the answer?
Add Answer to:
kindly make a java threaded program main class will accept n number and produce user inputed...
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
  • Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the ...

    Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the merge sort algorithm. The basic steps of merge sort are: 1) divide a collection of items into two lists of equal size, 2) use merge sort to separately sort each of the two lists, and 3) combine the two sorted lists into one sorted list. Of course, if the collection of items is just asingle item then merge sort doesn’t need to perform the three steps,...

  • java programe Write a program that prompts the user to enter in an integer number representing...

    java programe Write a program that prompts the user to enter in an integer number representing the number of elements in an integer array. Create the array and prompt the user to enter in values for each element using a for loop. When the array is full, display the following: The values in the array on a single line. The array with all of the elements reversed. The values from the array that have even numbered values. The values from...

  • write a java program Accept a positive integer n from keyboard and then create an array...

    write a java program Accept a positive integer n from keyboard and then create an array or arraylist containing n random elements within the range [-n,n). Print out the random array or arraylist, and then find out and print out the number of inversions and the maximum subarray (index range of the maximum subarray along with the maximum subarray sum) in the array or arraylist using divide and conquer, respectively. For example, suppose we accept integer 6 from keyboard, then...

  • JAVA PROGRAM. I need to write a program in java that will perform matrix addition and both matric...

    JAVA PROGRAM. I need to write a program in java that will perform matrix addition and both matrices have N rows and M columns, N>1 and M>1. The two matrices must be divided to four equal size sub-matrices and each sub-matrix has dimensions N/2 X M/2. I need to create four threads and each thread performs a sub-set of addition on one pair of the sub-matrices. I also need an extra thread for networking. The network part of this uses...

  • Draw the UML DIAGRAM ALSO PLEASE DRAW THE UML DIAGRAM.ALSO in java should use the program in java For this task you will create a Point3D class to represent a point that has coordinates in thr...

    Draw the UML DIAGRAM ALSO PLEASE DRAW THE UML DIAGRAM.ALSO in java should use the program in java For this task you will create a Point3D class to represent a point that has coordinates in three dimensions labeled x, y and z. You will then use the class to perform some calculations on an array of these points. You need to draw a UML diagram for the class (Point3D) and then implement the class The Point3D class will have the...

  • I have a multithreaded java sorting program that works as follows: 1. A list of double...

    I have a multithreaded java sorting program that works as follows: 1. A list of double values is divided into two smaller lists of equal size 2. Two separate threads (which we will term sorting threads) sort each sublist using a sorting algorithm of your choice 3. The two sublists are then merged by a third thread merging thread that merges the two sublists into a single sorted list. SIMPLE EXECUTION >java SortParallel 1000 Sorting is done in 8.172561ms when...

  • JAVA Programming Produce a method that reads in a set of values (double) from the user...

    JAVA Programming Produce a method that reads in a set of values (double) from the user and returns them. Use this header: public static double[] getNumsFromUser(String msg1, String msg2) The implementation of the method should start with a message to input the total number of array elements, followed by a message to enter all the array elements. The method returns the array of elements. The two strings msg1 and msg2 that are passed to the method as parameters will be...

  • The Language is Java GenericMethodTest (20) Download the OverloadedMethods.java program. Replace all the methods except main...

    The Language is Java GenericMethodTest (20) Download the OverloadedMethods.java program. Replace all the methods except main with a single generic method that produces the same output. Call the new file GenericMethodsTest.java. OverloadMethods.java: // Printing array elements using overloaded methods. public class OverloadedMethods { // method printArray to print Integer array public static void printArray(Integer[] inputArray) { // display array elements for (Integer element : inputArray) { System.out.printf("%s ", element); } System.out.printf("\n"); } // method printArray to print Double array public...

  • Design a Java program that asks the user to enter an integer number n and then...

    Design a Java program that asks the user to enter an integer number n and then generates an array of that many random points (x, y) with x- and y-coordinates in the range between 0 and 100. After this the program must ask the user to enter two diagonal points (x_1, y_1) and (x_2, y_2) of a rectangle with sides parallel to the coordinate axis (see the image below, where possible pairs of diagonal points are shown in red color)....

  • Programming 5_1: Create a Java class named <YourName>5_1 In this class create a main method...

    Programming 5_1: Create a Java class named <YourName>5_1 In this class create a main method, but for now leave it empty. Then write a Java method getAverage which takes an array of integers as it’s argument. The method calculate the average of all the elements in the array, and returns that average. The method should work for an array of integers of any length greater than or equal to one. The method signature is shown below: public static double getAverage(...

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