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

  • 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(){...

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

  • show work round 0.0001 Question 6 (1 point) You throw a stone at a certain angle...

    show work round 0.0001 Question 6 (1 point) You throw a stone at a certain angle with ground with velocity of V m/s. Total flight time is measured 4.35 sec and range is measured 166.96 m. Find projectile velocity V? Use g=10 m/s2 Your Answer: Answer units Question 7 (0.5 points) You throw a stone vertically upward from the roof of an 31 m tall building with a velocity of 50 m/s. How long after the stone will land the...

  • Java 1. Write a getCount method in the IntArrayWorker class that returns the count of the...

    Java 1. Write a getCount method in the IntArrayWorker class that returns the count of the number of times a passed integer value is found in the matrix. There is already a method to test this in IntArrayWorkerTester. Just uncomment the method testGetCount() and the call to it in the main method of IntArrayWorkerTester. 2. Write a getLargest method in the IntArrayWorker class that returns the largest value in the matrix. There is already a method to test this in...

  • JAVA basic quiz. 1. a) In order to run a java program, the computer must first...

    JAVA basic quiz. 1. a) In order to run a java program, the computer must first compile the .java files to create .class files. Then the Java Virtual Machine (JVM) runs the .class files. After compilation, when the JVM first starts to run the program, what is created in memory first? When do instances of an object get created? How long do they stay in memory? Under what circumstances will the JVM delete an object from memory? b)What are the...

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
Active Questions
ADVERTISEMENT