Question

How to code a program in java language were 2 threads run Thread #1 - IntegerSorter...

How to code a program in java language were 2 threads run

  • Thread #1 - IntegerSorter class - sorts 100k random integers (values ranging from 0-100,000)

  • Thread #2 - StringSorter class - sorts 30k element String array

  • Driver.java, which includes “main” and invokes both threads

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

Program

import java.time.Duration;
import java.time.Instant;
import java.util.Arrays;

class IntegerSorter implements Runnable {
   private int[] ar = new int[100000];
   private long timePassed;

   /* Loading array with random values */
   @Override
   public void run() {
       //for calculating time passed
       Instant start = Instant.now();
      
       for (int i = 0; i < ar.length; i++) {
           ar[i] = (int) (Math.random() * 100000);
       }
       System.out.println("Integer Array loaded successfully");
       Arrays.sort(ar);// sorting the array using inbuild quick sort
       System.out.println("Integer Array sorted successfully");
      
       Instant end = Instant.now();
       timePassed = Duration.between(start, end).toMillis();
   }

   public long getTimePassed() {
       return timePassed;
   }
}

class StringSorter implements Runnable {
   private String[] ar = new String[30000];
   private long timePassed;

   /* creating 4 letter arbitary words for string array */
   private void loadArray() {
       String letters = "abcdefghijklmnopqrstuvwxyz";
      
       for (int i = 0; i < ar.length; i++) {
           char ch1 = letters.charAt((int) (Math.random() * 26));
           char ch2 = letters.charAt((int) (Math.random() * 26));
           char ch3 = letters.charAt((int) (Math.random() * 26));
           char ch4 = letters.charAt((int) (Math.random() * 26));
           String word = ch1 + ch2 + ch3 + ch4 + "";// creating the word
           ar[i] = word;
       }
      
       System.out.println("String Array loaded Successfully");
   }

   @Override
   public void run() {
       Instant start = Instant.now();
      
       loadArray();
       Arrays.sort(ar);
       System.out.println("String Array sorted successfully");
      
       Instant end = Instant.now();
       timePassed = Duration.between(start, end).toMillis();
   }

   public long getTimePassed() {
       return timePassed;
   }
}

public class KuchBhi {
   public static void main(String[] args) throws InterruptedException {
      
       IntegerSorter obj = new IntegerSorter();
       StringSorter obj2 = new StringSorter();
       Thread IntArray = new Thread(obj);
       Thread StringArray = new Thread(obj2);
       // running the program
       IntArray.start();
       StringArray.start();
       IntArray.join();
       StringArray.join();
       System.out.println("Time taken to sort integer array: "+obj.getTimePassed()+" milli second");
       System.out.println("Time taken to sort string array: "+obj2.getTimePassed()+" milli second");
   }
}

Program Screenshots

Output

Add a comment
Know the answer?
Add Answer to:
How to code a program in java language were 2 threads run Thread #1 - IntegerSorter...
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
  • How to make a thread that sorts 100k random integers in java language Thread #1 -...

    How to make a thread that sorts 100k random integers in java language Thread #1 - IntegerSorter class - sorts 100k random integers (values ranging from 0-100,000)

  • 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,...

  • You will write a program using 2 threads to calculate the Fibonacci number. Each thread will...

    You will write a program using 2 threads to calculate the Fibonacci number. Each thread will process one of the values used to calculate the result for the last iteration of the calculation. Each thread will use a recursive function to calculate its value and store its result in a global array. Each thread will return its results in a global array. The main program must wait for both threads to complete before exiting (join method must be used). The...

  • Need help with this C program based on threads. If someone could help as soon as...

    Need help with this C program based on threads. If someone could help as soon as possible thanks in advance. Write a multi-threaded C program using pthread_create() according to the following instructions: Your program should create exactly 2 new threads in the main() function, each of which invokes a different function. Print the thread IDs of the two new threads after they are created. The first newly created thread invokes the following function void *print_string_in_reverse_order(void *str)  {       // this function accepts...

  • 1. Analyze the following code: public class Test implements Runnable { public static void main(String[] args) { Thread t = new Thread(this); t.start(); } public void run() { System....

    1. Analyze the following code: public class Test implements Runnable { public static void main(String[] args) { Thread t = new Thread(this); t.start(); } public void run() { System.out.println("test"); } } 1. The code compiles but will not print anything since t does not invoke the run method. 2. The code will not compile since you cannot invoke "this" in a static method. 3. The program compiles, runs, and prints tests on the console. 2. What will the following example...

  • Java Code please read comments and add the code to required lines....LINE 1 to LINE 5...

    Java Code please read comments and add the code to required lines....LINE 1 to LINE 5 ********************************************************************** * Program Summary: This program demonstrates these basic Java concepts: * - Creating an array based on user input * - Accessing and displaying elements of the array * * The program should declare an array to hold 10 integers. * The program should then ask the user for an integer. * The program should populate the array by assigning the user-input integer...

  • Language is Java. Thank you.It is a review test and I will find out the rest...

    Language is Java. Thank you.It is a review test and I will find out the rest if I just know that answers. 1) What is the output of the following code: public class Test public static void main(String] args) [ String s1 new String("Java String s2 new String("Java) System.out.print( (s1 s2)(s1.equals (s2))) A) false false B) true true C) false true D) true false E) None of the above 2) Given the following program: public class Test f public static...

  • Write a java program that declares 10 element array (of type integers), creates and initializes the...

    Write a java program that declares 10 element array (of type integers), creates and initializes the array, and perform the sum of elements of the array using for loop.   public class SumArray { public static void main (String[], args) { } // end of main } // end of SumArray class

  • Please use java Language Part I Implement a thread by extending the Thread class. Your thread...

    Please use java Language Part I Implement a thread by extending the Thread class. Your thread must do the following: (25 pts.) • Print its name and ID at the start. • Print the numbers from 1 to 100 that can be divided by 5. • Wait 1 second before printing a number. Part II Implement a thread by implementing the Runnable interface. Your thread must do the following: (25 pts.) • Print its name and ID at the start....

  • TRUE-FALSE     Basic synchronization principles and multithreading 1. Java user threads can implement both busy-waiting and no-busy-waiting...

    TRUE-FALSE     Basic synchronization principles and multithreading 1. Java user threads can implement both busy-waiting and no-busy-waiting policy. 2. Priority inversion avoids deadlocks. 3. Spinlock mutex can be used as an adaptive mutex. 4. Java RTE can be blocked for Input/Output operation. 5. Interrupted user thread, which executes a method in a monitor, must be rolled back to undo any changes it performed. 6. The synchronization primitive by disabling interrupts can be used by an application program. 7. Bounded-waiting requirement is...

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