...
But we also wanted to handle lambdas where allocated objects really did escape. Here is a simple example where we start with an array of longs and we want to produce an array of Strings, one for each long. You can build and run this using the instructions on Standalone Sumatra Stream API Offload Demo
package simplealloc;
import java.util.stream.IntStream;
import java.util.Random;
public class SimpleAlloc {
public static void main(String[] args) {
final int length = 20;
long[] input = new long[length];
String[] output = new String[length];
Random rand = new Random();
// Initialize the input array - not offloaded
IntStream.range(0, length).parallel().forEach(p -> {
input[p] = rand.nextLong();
});
// call toString on each input element - we mark this isas offloadable
IntStream.range(0, length).parallel().forEach(p -> {
output[p] = Long.toString(input[p]);
});
// Print results - not offloadable since it is
// calling native code etc.offloaded
IntStream.range(0, length).forEach(p -> {
System.out.println(output[p]);
});
}
}
...
Overview
Content Tools
ThemeBuilder