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

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

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

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

  • I want to write a C++ program which starts 2 threads. The first thread periodically generates...

    I want to write a C++ program which starts 2 threads. The first thread periodically generates a random number and adds it to the total. The second thread periodically generates a random number and subtracts it from the total. The main thread periodically monitors the value of the total. if the total exceeds the maximum value, the program terminates and alerts the user that the maximum has been exceeded. If the total undercuts the minimus value, the program terminates and...

  • Design Java (source code) a program (name it IndexOfLargest) to find the index of the first...

    Design Java (source code) a program (name it IndexOfLargest) to find the index of the first largest value in the array. Note that the largest value may appear more than once in the array. The program main method defines a single-dimensional array of size 10 elements and prompts the user to enter 10 integers to initialize the array. The main method then calls method findIndex() that takes a single-dimensional array of integer values and return the index of the first...

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