(Not logged on) | Log On
View  History

  

Creating arrays

7/28/2008 8:11 PM
You can subscribe to this wiki article using an RSS feed reader.

[Up to jsr166y.forkjoin Examples]

Parallel arrays are objects which are backed by actual arrays. You can create empty Parallel Arrays, create Parallel Arrays which are copies of existing arrays, or create Parallel Arrays which reference an existing array.

When you create a Parallel Array, you supply a ForkJoinExecutor (FJE) which is used to control the concurrent array tasks. This executor is also passed to derived Parallel Arrays. You can obtain the default FJE with ParallelArray.defaultExecutor(), or you can supply your own executor. You may not pass a null FJE to the Parallel Array create methods.

The examples below use ParallelDoubleArray, but analogous operation exist for ParallelArray<T> and ParallelLongArray.

Creating an empty Parallel Array

static final int SIZE = 1000;
...

ForkJoinExecutor fje = ParallelArray.defaultExecutor();

// this creates a Parallel Array with size() == 0 but with room
// for 1,000 items before it must reallocate.
ParallelDoubleArray pda = ParallelDoubleArray.createEmpty(SIZE, fje);

Creating a Parallel Array by copying an array

double[] array = new double[SIZE];

...

// Create a Parallel Array based on a copy of the input array.
// Modifying the ParallelArray will not alter the input array,
// and vice versa
ParallelDoubleArray pda = ParallelDoubleArray.createFromCopy(array, fje);

Creating a Parallel Array backed by an array

double[] array = new double[SIZE];

...

// Create a Parallel Array backed by the input array.
// Modifying the ParallelArray will alter the input array,
// and vice versa. You are advised to not reference the backing array
// after you hand it off to a Parallel Array, especially if
// the operations may resize or reallocate the array (at which point
// the two will no longer be linked). To enforce this, we assign
// null to the array object.
ParallelDoubleArray pda = ParallelDoubleArray.createUsingHandoff(array, fje);

array = null;

// operate on the Parallel Array

...


// When you have finished working with the Parallel Array, you can obtain
// the resulting array (and we null the Parallel Array reference)

array = pda.getArray();
pda = null;

David Biesack