Created
July 26, 2011 05:51
-
-
Save aruld/1106072 to your computer and use it in GitHub Desktop.
Slide # 15 in Martin Odersky's OSCON Java Keynote http://www.slideshare.net/Odersky/oscon-keynote-working-hard-to-keep-it-simple
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package oscon.java.parallel; | |
import extra166y.Ops; | |
import extra166y.ParallelArray; | |
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.List; | |
public class GoingParallelInJava { | |
ParallelArray<Person> people; | |
ParallelArray<Person> minors; | |
ParallelArray<Person> adults; | |
public GoingParallelInJava() { | |
jsr166y.ForkJoinPool fjPool = new jsr166y.ForkJoinPool(Runtime.getRuntime().availableProcessors()); | |
Person[] data = getSampleData(); | |
people = ParallelArray.createFromCopy(data, fjPool); | |
} | |
final Ops.Predicate<Person> isMinor = new Ops.Predicate<Person>() { | |
@Override | |
public boolean op(Person person) { | |
return person.age < 18; | |
} | |
}; | |
public void compute() { | |
minors = people.withFilter(isMinor).all(); | |
adults = people.removeAll(isMinor).all(); | |
} | |
// common code to both scala and java | |
public static Person[] getSampleData() { | |
List<Person> people = new ArrayList<>(); | |
for (int i = 1; i <= 10; i++) { | |
people.add(new Person("John Doe" + i, 30)); | |
people.add(new Person("Jane Doe" + i, 28)); | |
people.add(new Person("Baby Doe" + i, 5)); | |
} | |
return people.toArray(new Person[0]); | |
} | |
static class Person { | |
public final String name; | |
public final int age; | |
public Person(String name, int age) { | |
this.name = name; | |
this.age = age; | |
} | |
} | |
public static void main(String[] args) { | |
new GoingParallelInJava().compute(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment