# help in thred

• 05-02-2011, 12:48 AM
rickyjordan
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 ?

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

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.
• 05-02-2011, 01:10 AM
sunde887
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

Code:

```class Modifier implements Runnable{   public void run(){     //fill this in   } } class Average implements Runnable{   public void run(){     //fill this in   } }```
• 05-02-2011, 01:15 AM
rickyjordan
Do you have msn messenger or yahoo messenger ? plz
• 05-02-2011, 01:20 AM
sunde887
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.
• 05-02-2011, 01:25 AM
rickyjordan
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;     } }```
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 ?
• 05-02-2011, 01:30 AM
rickyjordan
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;     } }```
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 ?
• 05-02-2011, 01:30 AM
sunde887
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
Code:

```class Modifier implements Runnable{} class Averager implements Runnable{}```
• 05-02-2011, 01:52 AM
rickyjordan
its not working at all
• 05-02-2011, 01:56 AM
rickyjordan
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 ?
• 05-02-2011, 02:05 AM
sunde887
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.
• 05-02-2011, 02:21 AM
rickyjordan
help me i have to submit this assignment in few hours :(
• 05-02-2011, 02:23 AM
sunde887