Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: concurrent programming

  1. #1
    Junior Member
    Join Date
    Jun 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default concurrent programming

    Hi all,

    need a java code for three process concurrent program to find the mean of n numbers,
    if anyone knos, please rply

    thnks


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,434
    Thanks
    49
    Thanked 2,216 Times in 2,189 Posts

    Default Re: concurrent programming

    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.

  3. #3

  4. #4
    Junior Member
    Join Date
    Jun 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: concurrent programming

    Quote Originally Posted by Norm View Post
    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.
    yeah, need a help to write one

  5. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: concurrent programming

    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.

  6. #6
    Junior Member
    Join Date
    Jun 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: concurrent programming

    Hi All,

    still cudn't find or write a code ....can we do concurrent programming using threads?

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,434
    Thanks
    49
    Thanked 2,216 Times in 2,189 Posts

    Default Re: concurrent programming

    Shrt nsr, ys

  8. #8
    Member
    Join Date
    May 2010
    Posts
    38
    Thanks
    1
    Thanked 8 Times in 7 Posts

    Default Re: concurrent programming

    Quote Originally Posted by abc View Post
    Hi All,

    still cudn't find or write a code ....can we do concurrent programming using threads?
    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

  9. #9
    Junior Member
    Join Date
    Jun 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: concurrent programming

    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));
     
    		    }
     
     
    		    }	
     
    	}
     
    }

    /**
    * 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
    Last edited by Json; June 21st, 2010 at 02:55 AM. Reason: Please use code tags.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,434
    Thanks
    49
    Thanked 2,216 Times in 2,189 Posts

    Default Re: concurrent programming

    output of this code seems kind of mess
    Can you post the output and explain what is wrong with it?
    And explain what you want the output to be.

  11. #11
    Member Faz's Avatar
    Join Date
    Mar 2010
    Posts
    97
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Default Re: concurrent programming

    Quote Originally Posted by Norm View Post
    Is there a java code store near you that you could buy one of these from?

    Haha classic line.

    But yeah we need a bit more then "a mess" tell us what's happening maybe try to do some of your own debugging with println statements to see what is happening in what order.

Similar Threads

  1. Java Concurrent Programming
    By vzwsudha in forum Threads
    Replies: 6
    Last Post: May 27th, 2010, 04:26 AM
  2. Replies: 0
    Last Post: May 11th, 2010, 03:37 AM
  3. Is Java Programming Best?
    By messenger in forum Java IDEs
    Replies: 3
    Last Post: May 4th, 2010, 09:11 PM
  4. New to programming
    By Milanocookies56@gmail.com in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 31st, 2010, 09:55 AM
  5. Please help me with this Java programming
    By simply_stunning79 in forum Algorithms & Recursion
    Replies: 1
    Last Post: February 22nd, 2010, 06:48 PM