Results 1 to 13 of 13

Thread: help in thred

  1. #1
    rickyjordan is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default help in thred

    i have the following code to create random numbers and Sum the Avg

    i have to create thred for it and i dont know how , any one can help me ?

    Java Code:
    package project1;
    
    
    public class AverageExample
    {
        static int l = 100;
        public static int[] values;
        public static double average;
    
        public static void set_values() {
            int i;
            values = new int[l];
            for(i=0; i<l; i++) {
                values[i] = 1 + (int) (100.0*Math.random());
            }
        }
    	 public static void avg() {
            int sum=0, i;
            for(i=0; i<l; i++) {
                sum += values[i];
            }
            average = (double) sum / (double)l;
        }
    }
    You have two types of threads in your program:

    Modifers: A number of threads whose task is to put a random number in a random location in the array.
    Averager: A single thread whose task is to compute the average of the numbers of the array.

    Parallelism

    Modifiers can work in parallel as long as they don't modify the same location.
    When the Averager starts to compute the average, all Modifiers are blocked, i.e. critical section.

    Coding

    When the program starts, it must ask for the number of Modifiers the user wants to create.
    When a Modifier makes a change, it needs to print the changed location and the value.
    When the Averagefinishes, it must print the current average, and ask the user if he wants to terminate the program.
    It is easiest to create to independent classes for each type of thread (Modifier & Averager) and call them from your program.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I'm glad you came back, however; you need to ask more specific questions to get better help here. You stated your problem and it is quite broad. What exactly are you confused about? There is a lot of information in regards to threads and it is definitely not a simple topic.

    The assignment says it may be easier to create a class for the modifier, and for the averaged, so start here

    Java Code:
    class Modifier implements Runnable{
      public void run(){
        //fill this in
      }
    }
    
    class Average implements Runnable{
      public void run(){
        //fill this in
      }
    }
    Also, consider reading the tutorials on thread: Oracle Tutorials: Threading

  3. #3
    rickyjordan is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default

    Do you have msn messenger or yahoo messenger ? plz

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    No, but I am on these forums constantly. Ask away. Don't be embarrassed, if you aren't sure about something ask. Most people here are polite, and if the questions remain here you get a lot more people viewing them. This way if I am wrong, people can correct me.

    Just try your best, and we will help you out, we will push you to think about it but we will certainly help.

  5. #5
    rickyjordan is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default

    Java Code:
    package projectone;
    
    
    public class Average extends Modifier {
    
        public static void avg() {
            int sum=0, i;
            for(i=0; i<l; i++) {
                sum += values[i];
            }
            average = (double) sum / (double)l;
        }
    
    }
    Java Code:
    package projectone;
    
    
    public class Modifier {
    
        static int l = 100;
        public static int[] values;
        public static double average;
    
        public static void set_values() {
            int i;
            values = new int[l];
            for(i=0; i<l; i++) {
                values[i] = 1 + (int) (100.0*Math.random());
            }
    
    }
    }
    how i can immplement this in extend thred ?

  6. #6
    rickyjordan is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default

    Java Code:
    package projectone;
    
    
    public class Average{
    
        static int l = 100;
        public static int[] values;
        public static double average;
    
        public static void avg() {
            int sum=0, i;
            for(i=0; i<l; i++) {
                sum += values[i];
            }
            average = (double) sum / (double)l;
        }
    
    }
    Java Code:
    package projectone;
    
    
    public class Modifier {
    
        static int l = 100;
        public static int[] values;
        public static double average;
    
        public static void set_values() {
            int i;
            values = new int[l];
            for(i=0; i<l; i++) {
                values[i] = 1 + (int) (100.0*Math.random());
            }
    
    }
    }
    is that correct ?

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You make them a type of thread by making them implement runnable. Runnable is an interface which requires you to implement the method run. This is the threads worker method.

    The constructor of a thread takes a runnable, so if you have a class implement runnable it can be passed to the threads constructor.

    You should declare the threads like this
    Java Code:
    class Modifier implements Runnable{}
    class Averager implements Runnable{}

  8. #8
    rickyjordan is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default

    its not working at all

  9. #9
    rickyjordan is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default

    Java Code:
    import java.lang.runnable.run.();
    package projectone;
    
    
    public class Average implements Runnable{} {
    
        static int l = 100;
        public static int[] values;
        public static double average;
        
       
    
        public static void avg() {
            int sum=0, i;
            for(i=0; i<l; i++) {
                sum += values[i];
            }
            average = (double) sum / (double)l;
        }
    
    }
    is it correct ?

  10. #10
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You are getting closer. Since you implement runnable you must have a method called run. This is where the class does what it should do. Averager will find the average and print it, modifier will change something and print the change. You will not be able to do this assignment without a basic understanding of thread, synchronization, and critical sections. You are going to need to read the tutorials a bit.

    Threading is a very difficult topic, and unpredictable as well, my familiarity is not great and the tutorials will definitely help a lot.

  11. #11
    rickyjordan is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default

    help me i have to submit this assignment in few hours :(

  12. #12
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Please don't beg, I understand your situation but threading is not an easy topic. You need to try some more stuff, read a little about threads and put in some effort.

  13. #13
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

Similar Threads

  1. Replies: 1
    Last Post: 08-06-2007, 10:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •