This page describes the process of filling a byte array with encryption grade random numbers. We will be using Java Threads.
Background
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.
Results
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() {
attempt1();
attempt2();
}
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;
//System.out.println(endcount);
long start = System.currentTimeMillis();
for(int i=0; i< endcount; i++) {
es.execute(new Run(data, i));
}
es.shutdown();
try {
es.awaitTermination(3000, TimeUnit.SECONDS);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
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();
sr.nextBytes(data);
long end = System.currentTimeMillis();
//System.out.println(Arrays.toString(data));
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;
}
@Override
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);
sr.nextBytes(temp);
System.arraycopy(temp, 0, data, block*BLOCK_SIZE, remainder);
}
}
}