Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveAction;

public class Main {
    static int SEQ_THRESHOLD;

    public static void main(String[] args) throws Exception {
        int size = 100000;
        int[] v1 = new int[size];
        for (int i = 0; i < size; i++) {
            v1[i] = i;
        }
        for (SEQ_THRESHOLD = 10; SEQ_THRESHOLD < size; SEQ_THRESHOLD += 50) {
            double avgTime = 0.0;
            int samples = 5;
            for (int i = 0; i < samples; i++) {
                long startTime = System.nanoTime();
                ForkJoinPool fjp = new ForkJoinPool();
                fjp.invoke(new MyAction(0, size, v1));
                long endTime = System.nanoTime();
                double secsTaken = (endTime - startTime) / 1.0e9;
                avgTime += secsTaken;
            }
            System.out.println(SEQ_THRESHOLD + " " + (avgTime / samples));
        }
    }
}

class MyAction extends RecursiveAction {
    int[] v1;
    int start, end;

    MyAction(int start, int end, int[] v1) {
        this.start = start;
        this.end = end;
        this.v1 = v1;
    }

    int SEQ_THRESHOLD = Main.SEQ_THRESHOLD;

    @Override
    protected void compute() {
        if (end - start < SEQ_THRESHOLD) {
            for (int i = start; i < end; i++) {
                v1[i] = v1[i] + i * i;
            }
        } else {
            int mid = (start + end) / 2;
            invokeAll(new MyAction(start, mid, v1), new MyAction(mid, end, v1));
        }
    }
}