1 /* 2 * Copyright 2024 Hochschule Luzern Informatik. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package ch.hslu.exercises.sw11.ex3.fibo; 17 18 import java.util.concurrent.RecursiveTask; 19 20 /** 21 * Codevorlage für ein klassisches Beispiel zur Berechnung von Fibonacci Zahlen. 22 */ 23 public final class FibonacciTask extends RecursiveTask<Long> { 24 25 private final long n; 26 27 /** 28 * Erzeugt einen Fibonacci Task. 29 * 30 * @param n für die Fibonacci Berechnung. 31 */ 32 public FibonacciTask(final long n) { 33 this.n = n; 34 } 35 36 @Override 37 protected Long compute() { 38 if (n < 2) { 39 return n; 40 } 41 FibonacciTask f1 = new FibonacciTask(n - 1); 42 FibonacciTask f2 = new FibonacciTask(n - 2); 43 f1.fork(); 44 45 return f2.invoke() + f1.join(); 46 } 47 }