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 9032119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T07:54:07+00:00 2026-06-16T07:54:07+00:00

I was trying out the Java ForkJoin framework and wrote a simple test program

  • 0

I was trying out the Java ForkJoin framework and wrote a simple test program that sets the pixels of an image to random colors. E.g. it generates pseudo-noise.

But while testing performance I found that it’s actually faster to run single threaded than to run it with multiple threads. I make it run single threaded by passing a high threshold.

This is the class worker class:

public class Noise extends RecursiveAction {

    private BufferedImage image;
    private int xMin;
    private int yMin;
    private int xMax;
    private int yMax;
    private int threshold = 2000000; // max pixels per thread

    public Noise(BufferedImage image, int xMin, int yMin, int xMax, int yMax, int threshold) {
        this.image = image;
        this.xMin = xMin;
        this.yMin = yMin;
        this.xMax = xMax;
        this.yMax = yMax;
        this.threshold = threshold;
    }

    public Noise(BufferedImage image, int xMin, int yMin, int xMax, int yMax) {
        this.image = image;
        this.xMin = xMin;
        this.yMin = yMin;
        this.xMax = xMax;
        this.yMax = yMax;
    }

    @Override
    protected void compute() {
        int ppt = (xMax - xMin) * (yMax - yMin); // pixels pet thread
        if(ppt > threshold) {
            // split
            int verdeling = ((xMax - xMin) / 2) + xMin;
            invokeAll(new Noise(image, xMin, yMin, verdeling, yMax),
                    new Noise(image, verdeling+1, yMin, xMax, yMax));
        }
        else {
            // execute!
            computeDirectly(xMin, yMin, xMax, yMax);
        }
    }

    private void computeDirectly(int xMin, int yMin, int xMax, int yMax) {
        Random generator = new Random();
        for (int x = xMin; x < xMax; x++) {
            for (int y = yMin; y < yMax; y++) {
                //image.setPaint(new Color(generator.nextInt()));
                int rgb = generator.nextInt();
                int red = (rgb >> 16) & 0xFF;
                int green = (rgb >> 8) & 0xFF;
                int blue = rgb & 0xFF;

                red = (int) Math.round((Math.log(255L) / Math.log((double) red)) * 255);
                green = (int) Math.round((Math.log(255L) / Math.log((double) green)) * 255);
                blue = (int) Math.round((Math.log(255L) / Math.log((double) blue)) * 255);

                int rgbSat = red;
                rgbSat = (rgbSat << 8) + green;
                rgbSat = (rgbSat << 8) + blue;

                image.setRGB(x, y, rgbSat);
            }

        }
        Graphics2D g2D = image.createGraphics();
        g2D.setPaint(Color.RED);
        g2D.drawRect(xMin, yMin, xMax-xMin, yMax-yMin);
    }
}

When generating a 6000 * 6000 image the results are:
Single thread: 9.4sec @ 25% CPU load
Multi thread: 16.5sec @ 80%-90% CPU load
(Core2quad Q9450)

Why is the multi-threaded version slower?
How do I fix this?

  • 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-16T07:54:08+00:00Added an answer on June 16, 2026 at 7:54 am

    First of all, F/J is a niche product. If you don’t have a HUGE array and process it as a DAG then you’re using the wrong product. Sure, F/J can make use of multiple processors, but so can just using a simple multi-threaded approach without all the overhead of F/J.

    Try using four threads and just give each a quarter of the work directly.

    This is the way F/J was meant to be used:

    Sum left  = new Sum(array, low, mid);
    Sum right = new Sum(array, mid, high);
    left.fork();
    long rightAns = right.compute();
    long leftAns   = left.join();
    return leftAns + rightAns;
    

    When you don’t walk down the leaves of a structured tree, then all bets are off.

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

Sidebar

Related Questions

I'm trying out a very simple Java program that uses Scanner and its method
I'm trying out log4j in a simple test app. I create a new Java
I'm trying to figure out where java Applets that I run from the browser
I am trying out a simple program for printing characters. When I do this:
i just started learning swings. And thought of trying out a simple program, but
I am trying out the executor service in Java, and wrote the following code
I'm new to servers and databases. I've been trying to test out a java
I'm trying to figure out a Java regex problem that I'm having. This is
I am trying out a tool jhat here to test my java memory usage.
Hi I am trying out a simple java http server NanoHTTPD: http://elonen.iki.fi/code/nanohttpd/ Today I

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.