AhaExample.java

package ch.hslu.exercises.sw01.ex2;

public final class AhaExample {

    private AhaExample() {

    }

    private static int taskOneCalls = 0;
    private static int taskTwoCalls = 0;
    private static int taskThreeCalls = 0;
    //private static Logger LOG = LoggerFactory.getLogger(GGT.class);

    public static int task(final int n) throws InterruptedException {
        return task(n, false);
    }

    public static int task(final int n, final boolean testDuration) throws InterruptedException {
        task1(testDuration);
        task1(testDuration);
        task1(testDuration);
        task1(testDuration); // T ~ 4
        for (int i = 0; i < n; i++) { // äussere Schleife: n-mal
            task2(testDuration);
            task2(testDuration);
            task2(testDuration); // T ~ n · 3
            for (int j = 0; j < n; j++) { // innerer Schleife: n-mal
                task3(testDuration);
                task3(testDuration); // T ~ n · n· 2
            }
        }
//        LOG.info("Task 1 Aufrufe: " + TASK_1_CALLS);
//        LOG.info("Task 2 Aufrufe: " + TASK_2_CALLS);
//        LOG.info("Task 3 Aufrufe: " + TASK_3_CALLS);
        return taskOneCalls + taskTwoCalls + taskThreeCalls;
    }


    private static void task1(final boolean testDuration) throws InterruptedException {
        if (testDuration) {
            Thread.sleep(1);
        }
        taskOneCalls++;
    }

    private static void task2(final boolean testDuration) throws InterruptedException {
        if (testDuration) {
            Thread.sleep(1);
        }
        taskTwoCalls++;
    }

    private static void task3(final boolean testDuration) throws InterruptedException {
        if (testDuration) {
            Thread.sleep(1);
        }
        taskThreeCalls++;
    }
}