iRamble88 0 Newbie Poster
import java.util.Scanner;
class Sum
{
	private int value;

	public int get() {
		return value;
	}

	public void set(int sum) {
		this.value = sum;
	}
}

class Summation extends Thread
{
	private int upper;
        private int lower;
	private Sum sumValue;

	public Summation(int upper, Sum sumValue) {
		if (upper < 0)
			throw new IllegalArgumentException();

		this.upper = upper;
		this.sumValue = sumValue;
	}

	public void run()
	{
		int sum = 0;

		for (int i = 1; i >= lower && i <= upper; i++)
			sum += i;
		System.out.println(this.getName() + " is done");
		sumValue.set(sum);
		System.out.println(this.getName() + "  " + sum);
	}
}

public class MultithreadSum
{
        
	public static void main(String[] args)
	{
            
                Sum [] sumArray; //Array of type Sum?
                int k;
                
                
		Scanner sc = new Scanner(System.in);

		Sum sumObject = new Sum();
		int upper; // = 20; //Integer.parseInt(args[0]);
		System.out.println("Enter an integer value: ");
		upper = sc.nextInt();
		Summation[] worker = new Summation[10];
		for(int i = 0;i < 10;i++)
		{
		   worker[i] = new Summation(upper, sumObject);
		   Integer name = i+1;
		   worker[i].setName(name.toString());
		   worker[i].start();

		}
		/*for(int i = 0;i < 10;i++)
		{
			try
			{
				worker[i].join();
			}
			catch(InterruptedException e){System.out.println(e.toString());}
		}*/
		System.out.println("The value of " + upper + " is " + sumObject.get());
	}
}

I have almost no experience using Java and this is my Operating Systems assignment. Where I am really stuck is at adding an array of type Sum and adding up the Sum objects. New member so sorry in advance if I am doing anything wrong in posting this.

Modify the run method so that it adds between the lower and upper values inclusive.
Be sure to output the sum calculated by the thread along with its name.
4.In the main, make the following changes:
Add an array of type Sum.
Add an integer value to add up all the Sum objects.
Each worker thread will have its own Sum object so that the main can add them all up when the worker threads are done.
Prompt the user to enter the upper bound.  The threads are going to add up the numbers between 1 and the upper bound.
Prompt the user to enter the number of thread to create.
Make sure that the upper bound is greater than the number of threads and that the number of threads is greater than zero.
Loop through the number of threads sending each one a lower value and upper value and its own sumValue object.  Start each thread.
Make sure the main thread waits on each worker thread.
When all the worker threads are done, the main will add up each of their Sum objects and output the result.
To make sure your program is working correctly, calculate the actual sum which is n(n+1)/2.  This formula represents the sum of the integers between 1 and n.  Output this sum also.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.