Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6099085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:13:57+00:00 2026-05-23T13:13:57+00:00

Actually I wrote a Java program for calculating a particular number on the Fibonacci

  • 0

Actually I wrote a Java program for calculating a particular number on the Fibonacci series.

The problem is right now is that I am using number of cores as number of threads required. But I have observed that as the input size increases I get better performance by increasing the number of threads.

Is there an existing formula/Theory on how to divide the problem into multiple threads?

Below is the source code:

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class Fibonacci {
    private static long[] value;

    public static void main(String args[]) throws InterruptedException {
        int n;
        try {
            n = Integer.parseInt(args[0]);
        } catch (Exception e) {
            throw new RuntimeException(
                    "Please enter in the form java n number ");
        }
        value = new long[n + 1];


        long start = System.nanoTime();
        int nThreads = Runtime.getRuntime().availableProcessors();
        ExecutorService executorService = Executors
                .newFixedThreadPool(nThreads);
        int result;
        try {
            result = fibonacciSum(n, executorService);
        } catch (ExecutionException e) {
            throw new RuntimeException("Thread Interuppted ");
        }
        System.out.print(" MultiThreading = " + result);
        long end = System.nanoTime();
        System.out.println("\t time = " + (end - start) + "ns");
    }


    private static class FibonacciThread implements Runnable {
        int index;
        int result;
        ExecutorService executorService;

        public FibonacciThread(int index) {
            this.index = index;
        }

        public void run() {
            try {
                this.result = fibonacciSum(index, executorService);
            } catch (Exception e) {
                throw new RuntimeException("Thread interupted");
            }
        }
    }

    private static int fibonacciSum(int index, ExecutorService executorService)
            throws InterruptedException, ExecutionException {
        if (index <= 2) {
            return 1;
        } else {

            FibonacciThread fibonacciThread1 = new FibonacciThread(index - 2);
            fibonacciThread1.executorService = executorService;
            Future future = executorService.submit(fibonacciThread1);
            Object object = future.get();
            int resultPart2 = fibonacciSum(index - 1, executorService);
            int result = fibonacciThread1.result + resultPart2;
            // executorService.shutdown();
            return result;
        }
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T13:13:57+00:00Added an answer on May 23, 2026 at 1:13 pm

    In case you haven’t already realized this, Fibonacci numbers are not a good candidate for parallelization. You will get (increasingly) better performance:

    • by using a single threaded recursive algorithm,
    • by using a single threaded recursive algorithm with memoization, and
    • by solving the recurrence relation and implementing the resulting formula.

    The basic problem is that the naive Fibonacci calculation requires an exponential number of computation steps. You can’t make an impact on that (in terms of performance) with multi-threading because you have a bounded number of processors.

    Even if you did have an infinite number of processors, the thread setup / task creation overheads exceed the time for doing a step in the calculation.

    Finally, there is a particular problem with doing Fibonacci using the executor service and a bounded thread pool. If the thread pool is too small (or N is too large) the computation is liable to get stuck. For instance, the way that you’ve coded it needs ~N threads in the pool to calculate fibonacci(N), even though most of them will be blocked most of the time. (The best way to understand this is to “hand execute” the application, paying attention to how many threads are in use at each point in time … and what they are doing.)


    So the simple answer to your question is (for this particular application) that you need at least N threads in the pool to even complete the computation of Fibonacci(N). (This is not a general answer. It is mandated by the details of the algorithm that you’ve implemented.)


    There are lots of other SO questions / answers on calculating Fibonacci numbers using threads. I suggest you read them as well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I hava a Java program that needs to monitor a directory tree for changes.
Actually, I'm using this way. Do you have a better way? private bool AcceptJson(HttpRequest
I actually have two questions regarding the same problem but I think it is
I'm working on a 3d globe application written in java using java3d for the
I can access the database either from a .NET program (using ODBC) or through
How many people actually write an SDD document before writing a single line of
Actually, this question seems to have two parts: How to implement pattern matching? How
Actually here is a similar topic with little practical value. As far as I
Actually my question is all in the title. Anyway: I have a class and
Actually what i am trying to build is like a kind of firewall. It

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.