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

  • Home
  • SEARCH
  • 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 8917405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:28:32+00:00 2026-06-15T05:28:32+00:00

I have the following code which uses a single-threaded executor service, but running it

  • 0

I have the following code which uses a single-threaded executor service, but running it uses all the 4 cores on my machine (each core around 80% usage in average).

The question is why is this happening?
And I am not interested here in finding Fibonacci really!

public class MainSimpler {
    static int N=35;
    static AtomicInteger result = new AtomicInteger(0), pendingTasks = new AtomicInteger(1);
    static ExecutorService executor;

    public static void main(String[] args) {
        executor = Executors.newSingleThreadExecutor(); 
        long before = System.currentTimeMillis();
        System.out.println("Fibonacci "+N+" is ... ");
        executor.submit(new FibSimpler(N));
        waitToFinish();
        System.out.println(result.get());
        long after = System.currentTimeMillis();        
        System.out.println("Duration: " + (after - before) + " milliseconds\n");
    }

    private static void waitToFinish() {
        while (0 < pendingTasks.get()){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        executor.shutdown();
    }
}



class FibSimpler implements Runnable {
    int N;
    FibSimpler (int n) { N=n; }

    @Override
    public void run() {
        compute();
        MainSimpler.pendingTasks.decrementAndGet();
    }

    void compute() {
        int n = N;
        if (n <= 1) {
            MainSimpler.result.addAndGet(n);
            return;
        }
        MainSimpler.executor.submit(new FibSimpler(n-1));
        MainSimpler.pendingTasks.incrementAndGet();
        N = n-2;
        compute();  // similar to the F/J counterpart
    }
}

This is related to another question of mine.

  • 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-06-15T05:28:34+00:00Added an answer on June 15, 2026 at 5:28 am

    Just tried on my machine and I get 35% total CPU usage (4 cores). Note that you have at least 2 threads in your program (the main thread and the executor thread).

    However, if I increase N to 100, the CPU usage goes up to 90+% because a lot of time is spent in Full GCs (and I was running with 2GB of heap).

    So it looks like your single thread gets too busy and tasks start accumulating, waiting for being executed.

    You could try to run the code with the following JVM parameters: -XX:+PrintGC

    The output on my machine looks like:

    [GC 511999K->465632K(1962688K), 1.0286778 secs]
    [GC 977632K->922984K(1962688K), 1.1999209 secs]
    [GC 1434984K->1407984K(1962688K), 1.2421900 secs]
    [Full GC 1407984K->1373358K(1962688K), 9.8320408 secs]
    [Full GC 1885358K->1822040K(1962688K), 7.5170472 secs]
    [Full GC 1877375K->1870974K(1962688K), 7.6635945 secs]
    [Full GC 1877374K->1876550K(1962688K), 7.6705722 secs]
    [Full GC 1877374K->1877272K(1962688K), 7.8381579 secs]
    [Full GC 1877372K->1877357K(1962688K), 8.6095022 secs]

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

Sidebar

Related Questions

I have the following code which uses nested for-each loops to traverse two ArrayList
Hello I have the following code which uses knockout.js, var allJobberDetailsArray = []; getAllJobberDetailsArray();
I have the following code which re-uses a CookieContainer which logs in on the
I have the following code, which uses a stream to open and modify an
I have the following code, in which Boost.Local uses a function callback to load
I have following code which works for radio buttons but need to be changed
I have the following code which uses an array to write the result to
I have the following code which uses the System.Timers.Timer: // an instance variable Timer
I have the following code which uses Entity Framework: g_getWidgets = from getWidgets in
I have the following code which uses the WWW::Mechanize and HTML::TableExtract modules. Everything works

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.