0

I tried to write a program that prints sum of numbers from 1 to 100,000,000 using multithreading in Java. The idea is that the 1st thread will calculate the sum of the numbers from 1 to 25,000,000; the 2nd thread will calculate the sum of the numbers from 25,000,001 to 50,000,000, and so forth. The main thread will have to print out the sum after gathering the results. I tried extending the Thread class.

class Thread1 extends Thread{
    
    public void run() {
    //int i=num;
        int i;
    for(i=1;i<25000001;i++)
        i+=i;
    System.out.println("Thread 1: the sum is: "+i);
    
    mainThread m=new mainThread();
    m.guther(i);
    }
}

class Thread2 extends Thread{
    
    public void run() {
    //int i=num;
        int i;
    for(i=25000001;i<50000001;i++)
        i+=i;
    System.out.println("Thread 2: the sum is: "+i);
    
    mainThread m=new mainThread();
    m.guther(i);
    }
}


class Thread3 extends Thread{
    
    public void run() {
    //int i=num;
        int i;
    for(i=50000001;i<75000001;i++)
        i+=i;
    System.out.println("Thread 3: the sum is: "+i);
    
    mainThread m=new mainThread();
    m.guther(i);
    }
}

class Thread4 extends Thread{
    
    public void run() {
    //int i=num;
        int i;
    for(i=75000001;i<100000001;i++)
        i+=i;
    System.out.println("Thread 4: the sum is: "+i);
    
    mainThread m=new mainThread();
    m.guther(i);
    }
}

class mainThread{
    
    
    void guther(int total) {
        for(int i=1;i<=1;i++)
            total+=total;
        System.out.println("mainThread : the sum is: "+total);
        
    }
    /*public void run() {
        
      mainThread m=new mainThread();
        System.out.println(Thread.currentThread().getId()+"the total is: ");
    }*/
    
}

public class startThread {

    public static void main(String[] args) {
        //thread 1
        Thread1 t1=new Thread1();
        t1.start();
        //t1.run(1);
        
        //thread 2
        Thread2 t2=new Thread2();
        t2.start();
        //t2.run(25000001);
        
        //thread 3
        Thread3 t3=new Thread3();
        t3.start();
        //t3.run(50000001);
        
        //thread 4
        Thread4 t4=new Thread4();
        t4.start();
        //t4.run(75000001);
        
    //int total=t1.run(1)+t2.run(25000001)+t3.run(50000001)+t4.run(75000001);
    //System.out.println("the total is: "+total);
    
        //mainThread m=new mainThread();
    }
    
}
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95

2 Answers2

0

You're creating new instances of mainThread inside every single Thread implementation. This will be a problem, because they will not share the same result. You're also not keeping that result anywhere.

Multithreading is complex, and comes with a lot of issues, so I suggest you read on locks & synchronization of threads: https://www.tutorialspoint.com/java/java_multithreading.htm https://www.tutorialspoint.com/java/java_thread_synchronization.htm

[Edit] I got this wrong in the original answer. There are much simpler ways of calculating the sum of all of these: https://math.stackexchange.com/questions/593318/factorial-but-with-addition

Ferdz
  • 1,182
  • 1
  • 13
  • 31
0

You can use ForkJoinPool framework. Define the RecursiveTask that returns a value and sum the processed sub-results.

class SumRecursiveAction extends RecursiveTask<BigInteger> {

    private static final int PIVOT = 1000;

    private final int start;
    private final int end;
    private final List<Integer> list;

    public SumRecursiveAction(List<Integer> list, int start, int end) {
        this.start = start;
        this.end = end;
        this.list = list;
    }

    protected BigInteger compute() {
        if (end - start <= PIVOT) {
            // process if the range is within the pivot
            BigInteger sum = BigInteger.ZERO;
            for (int i = start; i < end; i++) {
                sum = sum.add(BigInteger.valueOf(list.get(i)));
            }
            return sum;
        } else {
            // or else delegate the task, fork and join
            int middle = start + ((end-start) / 2);
            RecursiveTask<BigInteger> otherTask = new SumRecursiveAction(list, start, middle);
            otherTask.fork();
            return new SumRecursiveAction(list, middle, end)
                    .compute()
                    .add(otherTask.join());
        }
    }
}

Usage:

List<Integer> list = IntStream.range(0, 100_000).boxed().collect(Collectors.toList());

ForkJoinTask<BigInteger> task = new SumRecursiveAction(list,0, list.size());
ForkJoinPool pool = new ForkJoinPool();
BigInteger sum = pool.invoke(task);


System.out.println("Sum: " + sum);
Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183