Hi all,
need a java code for three process concurrent program to find the mean of n numbers,
if anyone knos, please rply
thnks
Printable View
Hi all,
need a java code for three process concurrent program to find the mean of n numbers,
if anyone knos, please rply
thnks
Is there a java code store near you that you could but one of these from?
Or are you asking help to write one? In which case, tell us about the problems you are having.
what code/thoughts do you have so far?
If you're not sure where to start, Lord.Quackstar's post has a link to a fairly good tutorial on Java concurrent programming.
Hi All,
still cudn't find or write a code ....can we do concurrent programming using threads?
Shrt nsr, ys
Did you even read the guide that I showed you? We can't do this for you, you have to read it yourself. Go through each topic, reading and understanding the code. Don't just stop there, you also need to read the topics on locking and synchronization. Threading is hard and complex, and you must understand all of it if your going to do any type of debugging in the future. Once again:
Lesson: Concurrency (The Java™ Tutorials > Essential Classes)
And a little side note that I don't think the guide mentions: If your going to be using a Swing GUI, you need to do threading if you want your GUI to respond to your commands. Take a look at Improve Application Performance With SwingWorker in Java SE 6
Code :import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import java.util.Scanner; /** * This class contains code that will create three threads of execution concurrently * handling input of n numbers when program gets execution of its main method(). * n numbers are given in input array of args[] to main method. */ public class ConcMean extends Thread { // main method public static void main(String[] args) throws IOException{ BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.print("How many numbers in the list? "); int n = Integer.parseInt(in.readLine()); System.out.print("Enter " + n + " numbers to calculate the mean: "); List <Integer> numbers = new ArrayList<Integer>(); for (int i = 0; i < n; i++) { numbers.add(Integer.parseInt(in.readLine())); } System.out.println(); // calculate mean of n numbers parallel in three threads int count = numbers.size(); int pointer = count/3; int remainder = count%3; for (int i = 0; i < 3; i++){ // adding the values in segment 1 if (i == 0){ Thread thread1 = new MeanThread(numbers,0,pointer); thread1.start(); } else if (i==1){ Thread thread2 = new MeanThread(numbers,pointer,pointer); thread2.start(); } else { Thread thread3 = new MeanThread(numbers,(pointer*2),(pointer+remainder)); } } } }
Code :/** * class is used to calculate mean value of given n number array. Extending Thread * class enables concurrent caclulation of mean in parallel. */ class MeanThread extends Thread { private List numberList; int start; int count; public MeanThread(List numberList,int start,int count) { this.numberList = numberList; this.start = start; this.count = count; } //mean calculation public static void calcMean() { float mean = 0; int total = 0; Object numberArray[] = numberList.toArray(); for (int i=0; i<numberArray.length; i ++ ) { total = total + (Integer)numberArray[i]; System.out.println( " total: " + total); } mean = total / numberList.size(); System.out.println(" Mean: " + mean); } } public void run() { calcMean(); }
the output of this code seems kind of mess..anyone guess what is the wrong with this code
Can you post the output and explain what is wrong with it?Quote:
output of this code seems kind of mess
And explain what you want the output to be.