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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:17:26+00:00 2026-06-09T03:17:26+00:00

I have code that consumes a large number (millions currently, eventually billions) of relatively

  • 0

I have code that consumes a large number (millions currently, eventually billions) of relatively short (5-100 elements) arrays of random numbers and does some not-very-strenuous math with them. Random numbers being, well, random, ideally I’d like to generate them on multiple cores, since random number generation is > 50% of my runtime in profiling. However, I’m having difficulty distributing a large number of small tasks in a way that’s not slower than the single-threaded approach.

My code currently looks something like this:

for(int i=0;i<1000000;i++){
    for(RealVector d:data){
        while(!converged){
            double[] shortVec = new double[5];
            for(int i=0;i<5;i++) shortVec[i]=rng.nextGaussian();
            double[] longerVec = new double[50];
            for(int i=0;i<50;i++) longerVec[i]=rng.nextGaussian();
            /*Do some relatively fast math*/
        }
    }
}

Approaches I’ve taken that have not worked are:

  • 1+ threads populating an ArrayBlockingQueue, and my main loop consuming and populating the array (the boxing/unboxing was killer here)
  • Generating the vectors with a Callable (yielding a future) while doing the non-dependent parts of the math (it appears the overhead of the indirection outweighed whatever parallelism gains I got)
  • Using 2 ArrayBlockingQueue, each populated by a thread, one for the short and one for the long arrays (still roughly twice as slow as the direct single-threaded case).

I’m not looking for “solutions” to my particular problem so much as how to handle the general case of generating large streams of small, independent primitives in parallel and consuming them from a single thread.

  • 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-09T03:17:28+00:00Added an answer on June 9, 2026 at 3:17 am

    This is more efficient than using a Queue because;

    • the payload is an array of double[] meaning the background thread can generate more data before having to pass it off.
    • all the objects are recycled.

    .

    public class RandomGenerator {
        private final ExecutorService generator = Executors.newSingleThreadExecutor(new ThreadFactory() {
            @Override
            public Thread newThread(Runnable r) {
                Thread t = new Thread(r, "generator");
                t.setDaemon(true);
                return t;
            }
        });
        private final Exchanger<double[][]> exchanger = new Exchanger<>();
        private double[][] buffer;
        private int nextRow = Integer.MAX_VALUE;
    
        public RandomGenerator(final int rows, final int columns) {
            buffer = new double[rows][columns];
            generator.submit(new Callable<Void>() {
                @Override
                public Void call() throws Exception {
                    Random random = new Random();
                    double[][] buffer2 = new double[rows][columns];
                    while (!Thread.interrupted()) {
                        for (int r = 0; r < rows; r++)
                            for (int c = 0; c < columns; c++)
                                buffer2[r][c] = random.nextGaussian();
                        buffer2 = exchanger.exchange(buffer2);
                    }
                    return null;
                }
            });
        }
    
        public double[] nextArray() throws InterruptedException {
            if (nextRow >= buffer.length) {
                buffer = exchanger.exchange(buffer);
                nextRow = 0;
            }
            return buffer[nextRow++];
        }
    }
    

    Random is thread safe and synchronized. This means each thread needs it own Random to perform concurrently.

    how to handle the general case of generating large streams of small, independent primitives in parallel and consuming them from a single thread.

    I would use an Exchanger<double[][]> to populate values in the background as pass them efficiently (without much GC overhead)

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

Sidebar

Related Questions

I run an affiliate website, and I have code that consumes the XML datafeeds.
I have a bunch of Workflow foundation 4.0 RC code activities that consume web
I have code that generates a List<string[]> variable but can't quite figure out how
I have code that looks more or less like the code below but it
I have code that sends web requests through two parallel for each loops. Will
I have code that reloads images via HTTP from the main thread and displays
I have code that looks like this: obj.foo(); // obj might, or might not
I have code that looks like this: template<class T> class list { public: class
I have code that I want to look like this: List<Type> Os; ... foreach
I have code that uses Win API function RegSaveKeyEx to save registry entries to

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.