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

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 developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.