|
|
[Up to jsr166y.forkjoin Examples]
A placeholder page for my code snippets and usage examples.
1. ParallelToLowerCase
Description: This simple code gets an arbitrary String array and applies a lower case method to each element and prints the array on screen.
// begin the code
import jsr166y.forkjoin.ParallelArray;
import static jsr166y.forkjoin.Ops.*;
public class ParallelToLowerCase {
public static void main(String[] args) {
final String[] list = new String[] {"AB", "CD", "EF", "GH", "MN",
"RM", "JP", "LS", "QP", "TX",
"ST", "SZ", "AA", "PQ", "RS",
"RM", "JP", "LS", "QP", "TX",
"ST", "SZ", "AA", "PQ", "RS",
"RM", "JP", "LS", "QP", "TX",
"ST", "SZ", "AA", "PQ", "RS",
"RM", "JP", "LS", "QP", "TX",
"CO", "BA", "MP", "AM", "FF"};
// creates a ParallelArray
// Note: Since the 'list' array is not used after this point, you can also
// use ParallelArray.createUsingHandoff(). See Creating arrays
ParallelArray<String> pArray =
ParallelArray.createFromCopy(list, ParallelArray.defaultExecutor());
pArray.replaceWithMapping(new ToLowerMapping());
System.out.println(pArray.asList());
}
// Mapping class
public static class ToLowerMapping implements Op<String, String> {
@Override
public String op(String input) {
return input.toLowerCase();
}
// Note: Mappers are often immutable objects, so to reduce
// needless instantiation, a Singleton can be used and the constructor made private.
// Or the entire mapper class hidden completely and the mapper exposed only
// through a factory method, as is done in CommonOps
}
}
// End of code
2. ForkJoinTreeSearch
Coming soon.