Filling a byte array with Random Numbers using Threads

This page describes the process of filling a byte array with encryption grade random numbers. We will be using Java Threads.


This little project started out because I wanted to see how fast my computer can fill an array with random numbers. When I finished implementation described in attempt1() method, I saw that only one of the 2 processors on my machine was at 100%. I was thinking if I break up the process in 2 threads the operating system would assign both processors and I would get the job done in 1/2 the time.


The results are disappointing… It seems that both processors were used but it too a little more time than the single threaded mode. I suspect that this was due some array copy operations i had to do. I played with the constants at the top of the class but the process took a few hundred milliseconds longer than it would have if it was single threaded.

Request for Help

If you guys know a better way to utilize multi-threads to achieve the same results faster please let me know by posting some code in the comments box below.

Complete source code for the test program included. Below

package com.test;

import java.security.SecureRandom;
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class App2 {
	private static final int DATA_SIZE = 1024*100000;
	private static final int BLOCK_SIZE = 8024;
	private static final int NUM_OF_THREADS = 16;
	private static final SecureRandom sr = new SecureRandom();

	public App2() {
	public static void main(String args[]) {
		new App2();
	private void attempt2() {
		System.out.println("Attempt 2");
		int size = DATA_SIZE;
		//System.out.printf("size of array: %s\n", new DecimalFormat("###,###,###,###").format(size));
		final byte data[] = new byte[size];
		ExecutorService es = Executors.newFixedThreadPool(NUM_OF_THREADS);
		int endcount = (size/BLOCK_SIZE) + 1;
		long start = System.currentTimeMillis();
		for(int i=0; i< endcount; i++) {
			es.execute(new Run(data, i));		
		try {
			es.awaitTermination(3000, TimeUnit.SECONDS);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
		long end = System.currentTimeMillis();
		System.out.printf("System took %d ms to process.\n", end - start);

	 * Tried to do it all in one shot. with a single thread
	 * average time 22.7 seconds.
	private void attempt1() {
		System.out.println("Attempt 1");
		int size = DATA_SIZE;
		System.out.printf("size of array: %s\n", new DecimalFormat("###,###,###,###").format(size));
		byte data[] = new byte[size];
		long start = System.currentTimeMillis();
		long end = System.currentTimeMillis();
System.out.printf("System took %d ms to process.\n", end - start);
	class Run implements Runnable {
		private int block;
		private byte[] data;
		public Run(byte[] data, int block) {
			this.block = block;
			this.data = data;
		public void run() {
			byte[] temp = new byte[BLOCK_SIZE];  // 8k block
			int remainder = 0;
			if(block*BLOCK_SIZE+BLOCK_SIZE > data.length) {
				// get the remainder
				remainder = data.length % (block*BLOCK_SIZE);
			} else {
				remainder = BLOCK_SIZE;
			//System.out.println("remainder: " + remainder);
			System.arraycopy(temp, 0, data, block*BLOCK_SIZE, remainder);


0 Responses to “Filling a byte array with Random Numbers using Threads”

  1. Leave a Comment

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 77 other followers

April 2010
« Mar   May »

Blog Stats

  • 846,580 hits

%d bloggers like this: