Question

Java: use fork and join to count from 0.0 to 1 million by increments of 0.0001...

Java:

use fork and join to count from 0.0 to 1 million by increments of 0.0001 and print how long its takes

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

`Hey,

Note: Brother in case of any queries, just comment in box I would be very happy to assist all your queries

File : Step.java

package javaforkjoin;
import java.util.concurrent.ForkJoinPool;

import org.apache.commons.lang3.time.StopWatch;

public class Step {
   static double max = 1000000;
   static double inc = 0.0001;
   public static void main(String[] args) {
      
       method1();
       method2();
      
   }
   private static void method1() {
          
       double d = 0.0;
       StopWatch sw = new StopWatch();
       sw.start();
       while(d < max)
       {
           d += inc;
       }
       sw.stop();
      
       System.out.println("Method 1:"+sw.getTime());
   }
  
   private static void method2() {
       ForkJoinPool pool = new ForkJoinPool();
      
       CounterProcessor forktask = new CounterProcessor(max);

       StopWatch sw = new StopWatch();
       sw.start();
       Double taskResult = pool.invoke(forktask);
sw.stop();
      
       System.out.println("Method 2:"+sw.getTime());
       System.out.println("taskResult = " + taskResult);
      
   }

}

File : Counterprocessor.java

package javaforkjoin;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.RecursiveTask;

import org.apache.commons.lang3.time.StopWatch;

public class CounterProcessor extends RecursiveTask<Double> {

   private Double count = 0.0d;
   static double max = 10000;
   static double inc = 0.0001;
   public CounterProcessor(Double result) {
this.count = result;
}
  
   @Override
   protected Double compute() {
       // TODO Auto-generated method stub
       Double d = 0.0d;
      
       if(this.count > 10001.0d)
       {
           List<CounterProcessor> subtasks =
       new ArrayList<CounterProcessor>();
             
       subtasks.addAll(createSubtasks());

       for(CounterProcessor subtask : subtasks){
       subtask.fork();
       }

       Double result = 0.0d;
       for(CounterProcessor subtask : subtasks) {
       result += subtask.join();
       }
       return result;
       }
       else
       {
           double d1 = 0.0;
          
           while(d1 < max)
           {
               d1 += inc;
           }
           return d1;
       }
      
   }
  
   private List<CounterProcessor> createSubtasks() {
List<CounterProcessor> subtasks =
new ArrayList<CounterProcessor>();
  
  
for(int i = 0; i<10;i++){
subtasks.add(new CounterProcessor(this.count / 10   ));
}
return subtasks;
}

}

Library Used: org.apache.commons.lang3.time.StopWatch

Result on 4 core processor

Method 1:19232
Method 2:7162
taskResult = 1000000.0099198519

Kindly revert for any queries

Thanks.

Add a comment
Know the answer?
Add Answer to:
Java: use fork and join to count from 0.0 to 1 million by increments of 0.0001...
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
  • Write a single command line to accomplish the following tasks in Python 1- Count the number...

    Write a single command line to accomplish the following tasks in Python 1- Count the number of instances of each item in a list and produce another list of lists containing each item and its count. (Use the list count method) 2- Remove all words containing a capital letter at the end in a given string. (Use the join and ends with methods) 3- Remove every other letter from a string

  • Using C++; Write and execute a program in C++ that will count from 1 to 21...

    Using C++; Write and execute a program in C++ that will count from 1 to 21 and print the count, and its cubed value, for each count. 1 1 2 8 3 27 etc..

  • Implement a program that will count from N (provided by the user) to 1 and by...

    Implement a program that will count from N (provided by the user) to 1 and by the end of it show the following message: I’ve learned how to use recursion!. The recursive function should be responsible to print out the recursion message, not the main (you are required to do this program using recursive approach)

  • please use java application to solve methods: 5,6,10,12,13 (no need for the rest) Problem1: Create a...

    please use java application to solve methods: 5,6,10,12,13 (no need for the rest) Problem1: Create a new Java Application that has the following methods: 6 A method that generate a binary search tree (BST) where the number of nodes and the range of the values are 1. from a file. 2. A recursive method to print the BST in preorder traversal 3. A recursive method to print the BST in post-order traversal 4. A recursive method to print the BST...

  • Problem 4: Write a Java program that reads positive integers, find the largest of them, count...

    Problem 4: Write a Java program that reads positive integers, find the largest of them, count its occurrences and print out the average of all input integers up to two decimal places (小數 點第二位). Use the characters·Q' or'q, to end the input. Below are two sample runs: Enter an integer, or quit with Q or q:1 Enter an integer, or quit with Q or q: 2 Enter an integer, or quit with Q or q: 3 Enter an integer, or...

  • 1. How many successive velocity increments of e are needed to produce a resultant velocity of...

    1. How many successive velocity increments of e are needed to produce a resultant velocity of 0.99e (Hint: You can do this using the standard form of the velocity addition law in the book. But it is easier if you use the fact that, in terms of the rapidities φ,a, a, defined by : ctanho, u-: c tanha, u' =: c tanha", the velocity addition law takes the simple forn。' + φ. If you do it this second way, tanh...

  • 15.6: Fix the code to print the count from 1 to x (to loop x times)...

    15.6: Fix the code to print the count from 1 to x (to loop x times) #include <iostream> // include the header file using namespace std; void count(int x){    for(int i=1; i<=x; i++){ cout<<x; } } int main(){    int x,i;    cin >> x; count(x);    return 0; } 15.7 Fix the nested for loops to print the count from 1 to x twice, e.g. "12....x12.....x" #include <iostream> // include the header file using namespace std; int main(){...

  • PLEASE USE R FOR THE FOLLOWING! I NEED THE CALCULATIONS FROM R TO THE NEAREST 0.0001...

    PLEASE USE R FOR THE FOLLOWING! I NEED THE CALCULATIONS FROM R TO THE NEAREST 0.0001 FOR PROBABILITIES. 2. A company producing fine crystal knows that 19% of its goblets have cosmetic flaws and must be classified as “seconds.” Consider a random sample of 620 goblets. In a – c use the Binomial distribution to compute the probability that a. exactly 120 are flawed (use the d function) b. at most 130 are flawed (use the p function – as...

  • <Java> 1-Write a program that converts from a string into an integer number. For example: For...

    <Java> 1-Write a program that converts from a string into an integer number. For example: For "123" the program should return 123 For "131", The program should return 131 For "-121" the program should return -121 For "1e3" the program should print an Invalid number For "a1" the program should print an invalid number Do not use Integer.parse function 2. Generate a random number in [0, 100]. Write an efficient guess procedure that takes no more than 7 guesses to...

  • USING PYTHON 1. Write a small program that asks for an integer number from the user...

    USING PYTHON 1. Write a small program that asks for an integer number from the user and print all the prime numbers (2,3,5,7,etc) less than the input number. 2. How long it takes for your program to print the prime numbers less than 100. (Use magic functions)

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