Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Create a multi-threaded program that reads an integer value n from the user (you can assume any constant) and finds the sum of numbers from 1 to n using 4 new threads, where each thread computes only ¼ of the sum. Main thread prints out the final sum.

What I have tried:

Java
import java.util.Scanner;

class sum implements Runnable {
    double a, b;

    public sum(double a, double b) {
        this.a = a;
        this.b = b;
    }

    public void run() {
        add(a, b);

    }

    public void add(double a, double b) {
        double sum = 0;
        synchronized (this) {
            for (double i = a; i <= b; i++) {
                sum = sum + i;

            }
            System.out.println("Sum of " + a + " to " + b + " numbers = " + (double) sum);
        }

    }
}

public class Main {
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();

        Thread t1 = new Thread(new sum((double) 1, (double) n / 4));
        Thread t2 = new Thread(new sum((double) (n / 4) + 1, (double) n / 2));
        Thread t3 = new Thread(new sum((double) (n / 2) + 1, (double) n * 3 / 4));
        Thread t4 = new Thread(new sum((double) (n * 3 / 4) + 1, (double) n));
        Thread main = new Thread(new sum((double) 1, (double) n));

        t1.start();
        t2.start();
        t3.start();
        t4.start();
        main.start();

        try {
            t1.join();
            t2.join();
            t3.join();
            t4.join();
            main.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}
Posted
Updated 6-May-20 23:30pm
v3
Comments
ZurdoDev 6-May-20 14:35pm    
There are many, so many, articles on how to do threads in C# or C. I'm not sure what you were looking for, but they are very easy to find.

If indeed it is C, this is the very first google result for me, https://www.geeksforgeeks.org/multithreading-c-2/
KarstenK 8-May-20 5:10am    
You have some type cast flaw in the calculation of your sum constructors, because your are dividing integer and are casting the result.

I strongly recommend that your n is a double!!!

Unfortunately threads programming is Operative System dependent. However Posix Threads[^] are widely supported and you may easily find info about (see, for instance: POSIX Threads Programming[^]).
 
Share this answer
 
This is one of the many questions that makes me wonder what the instructor is actually trying to accomplish. I will not tell you how to do it, but the idea is to have each thread work in sequence. That is, the second thread must wait until the first thread is finished - I know this does not seem to make sense, but go with it. Eventually this frees the 1st thread to allow it to handle the next input (request). So, instead of waiting for a thread to completely process its calculations, you can start the next.

Think about it.
 
Share this answer
 
Comments
Richard MacCutchan 7-May-20 5:33am    
"makes me wonder what the instructor is actually trying to accomplish."
Many of us here wonder the same thing regularly. And why on earth are they using double types as beginners, especially as loop counters?
Jhon Edicen 7-May-20 6:28am    
Instructor wants to check to understanding of properly usage of threads ( like he said this). In this task he wants us to write a program which gets input from user, for example 5, and our program should have 4 threads which calculate the sum, 1+2+3+4+5. But the main thing is, he wants from us that each thread should only perform 1/4 of the processes, in out case each thread only should do 1/4 of sum operation and the main thread should print the sum.
You have tagged your question "C" but your code is Java: I will correct it. You can learn all about threads in Java at Lesson: Concurrency (The Java™ Tutorials > Essential Classes)[^]

You could also simplify the code by using integer types rather than doubles, especially since you are only calculating simple sums.
 
Share this answer
 
v2
Comments
Richard MacCutchan 7-May-20 6:32am    
The first thing you need to do is to write the simple methods to calculate the various sums, and then collect them together. So start with a method that takes two numbers (use integers to make it clear) and returns the sum of all the numbers in that sequence. So if you pass it the two values 7 and 11 it will return 45, i.e. 7 + 8 + 9 + 10 + 11. Once you have a working method you can go onto the next stage of accepting a number from the user and doing the same thing. Finally you need to learn about how the threads can interact and process part of the sum. Stay with integers as they are so much easier to work with, and try just two threads. Once you have that working add the other threads and test the whole thing. You can ensure it works with integers by always passing a number that is divisible by 4.

Don't try to write the complete program from the start, build it up section by section, just as you would with creating something physical like a house or a car.
Richard MacCutchan 7-May-20 7:12am    
I cannot tell you whether it is acceptable to submit because I am not your instructor. The first question is: does it produce the correct answer, and does it use threads to produce that answer? If no, then it needs refining based on what answers it does produce.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900