Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Main {
- private static final ForkJoinPool mainPool = new ForkJoinPool();
- public static final int NUMBER = Integer.MAX_VALUE;
- public static void main(final String[] args) {
- new Main().doIt();
- }
- protected void doIt() {
- final List<First> firsts = createFirsts();
- System.out.println("Sequential");
- final Stopwatch sequentialStopWatch = SimonManager.getStopwatch("sequential");
- for (int i = 0; i < 5000; i++) {
- findNumberSequentially(firsts);
- }
- System.out.println(sequentialStopWatch);
- System.out.println("Parallel");
- final Stopwatch parallelStopWatch = SimonManager.getStopwatch("parallel");
- for (int i = 0; i < 5000; i++) {
- findNumberUsingParallelism(firsts);
- }
- System.out.println(parallelStopWatch);
- }
- protected List<First> createFirsts() {
- System.out.print("Generating some data...");
- final List<First> firsts = Lists.newArrayList();
- for (int i = 0; i < 10; i++) {
- final First first = new First();
- firsts.add(first);
- }
- final First specialFirst = new First();
- final Second specialSecond = new Second();
- final Third specialThird = new Third(NUMBER);
- specialSecond.addThird(specialThird);
- specialFirst.addSecond(specialSecond);
- firsts.add(specialFirst);
- System.out.println("done!");
- return firsts;
- }
- protected void findNumberSequentially(final List<First> firsts) {
- final Split split = SimonManager.getStopwatch("sequential").start();
- doSequentially(firsts);
- split.stop();
- }
- protected void doSequentially(final List<First> firsts) {
- for (final First first : firsts) {
- for (final Second second : first.getSeconds()) {
- for (final Third third : second.getThirds()) {
- if (third.getValue() == NUMBER) {
- return;
- }
- }
- }
- }
- }
- protected void findNumberUsingParallelism(final List<First> firsts) {
- final Split split = SimonManager.getStopwatch("parallel").start();
- doInParallel(firsts);
- split.stop();
- }
- protected void doInParallel(final List<First> firsts) {
- for (final First first : firsts) {
- for (final Second second : first.getSeconds()) {
- if (mainPool.invoke(new FinderTask(second.getThirds()))) {
- return;
- }
- }
- }
- }
- }
- public class First {
- private List<Second> seconds = Lists.newArrayList();
- public First() {
- for (int i = 0; i < 100; i++) {
- final Second second = new Second();
- seconds.add(second);
- }
- }
- public void setSeconds(final List<Second> seconds) {
- this.seconds = seconds;
- }
- public void addSecond(final Second second) {
- seconds.add(second);
- }
- public List<Second> getSeconds() {
- return seconds;
- }
- }
- public class Second {
- private List<Third> thirds = Lists.newArrayList();
- public Second() {
- for (int i = 0; i < 50000; i++) {
- final Third third = new Third(i);
- thirds.add(third);
- }
- }
- public void setThirds(final List<Third> thirds) {
- this.thirds = thirds;
- }
- public void addThird(final Third third) {
- thirds.add(third);
- }
- public List<Third> getThirds() {
- return thirds;
- }
- }
- public class Third {
- private final int value;
- public Third(final int value) {
- this.value = value;
- }
- public int getValue() {
- return value;
- }
- }
- public class FinderTask extends RecursiveTask<Boolean> {
- private static final long serialVersionUID = 8711505823183192823L;
- private static final int THRESHOLD = 100;
- private final List<Third> thirds;
- public FinderTask(final List<Third> thirds) {
- this.thirds = thirds;
- }
- @Override
- protected Boolean compute() {
- if (thirds.size() < THRESHOLD) {
- return solveSequentially();
- }
- final List<List<Third>> partitioned = Lists.partition(thirds, thirds.size() / 2);
- final FinderTask[] finderTasks = new FinderTask[partitioned.size()];
- for (int i = 0; i < partitioned.size(); i++) {
- finderTasks[i] = new FinderTask(partitioned.get(i));
- }
- invokeAll(finderTasks);
- for (final FinderTask finderTask : finderTasks) {
- if (finderTask.isCompletedNormally() && finderTask.getRawResult()) {
- return true;
- }
- }
- return false;
- }
- private boolean solveSequentially() {
- for (final Third third : thirds) {
- if (third.getValue() == Main.NUMBER) {
- return true;
- }
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement