The problem I am trying to solve is

Write a program using fork() to compute average of N numbers. The parent process forks a child process that takes input from the user, adds all the numbers and returns the sum and the count of numbers to the parent process that divides the sum by the count of the number, and prints out the average.

What I need help with is how to go about doing this in java. I know you need to use threads instead of fork(). Do I just make two threads, one that takes the user input and sums it and one that takes the sum and finds the average? Also if this is how you would do it how do you go about passing the sum and the count to the parent thread. I having trouble figuring out how to pass data between threads

Recommended Answers

The previous post answers how to do this in any version of Java.
If you are lucky enough to be on the recent Java 7 (version 1.7) you could also use the new fork/join framework that was introduced with Java 7. However, it doesn't exist in any earlier version

Jump to Post

All 2 Replies

The problem I am trying to solve is

Write a program using fork() to compute average of N numbers. The parent process forks a child process that takes input from the user, adds all the numbers and returns the sum and the count of numbers to the parent process that divides the sum by the count of the number, and prints out the average.

What I need help with is how to go about doing this in java. I know you need to use threads instead of fork(). Do I just make two threads, one that takes the user input and sums it and one that takes the sum and finds the average? Also if this is how you would do it how do you go about passing the sum and the count to the parent thread. I having trouble figuring out how to pass data between threads

Below link will explain very clearly on sharing the data between thread. Hope it helps you.

http://www.java2s.com/Code/Java/Threads/Theproducerconsumerapproachtothreadcooperation.htm

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.