Commit e3b8881d by Patryk Czarnik

pierwsze lambdy

parent 13bb6d93
......@@ -57,7 +57,7 @@ public class ForkJoin1_SumArray {
final ThreadLocalRandom random = ThreadLocalRandom.current();
System.out.println("Losowanie...");
for(int i = 0; i < tab.length; i++)
tab[i] = (byte)random.nextInt(256);
tab[i] = (byte)random.nextInt(128);
Long result;
System.out.println("\nLiczenie sekwencyjne:");
......
......@@ -59,7 +59,7 @@ public class ForkJoin2_SumArray_Limit_Get {
final ThreadLocalRandom random = ThreadLocalRandom.current();
System.out.println("Losowanie...");
for(int i = 0; i < tab.length; i++)
tab[i] = (byte)random.nextInt(256);
tab[i] = (byte)random.nextInt(128);
Long result;
final ForkJoinPool pool = new ForkJoinPool(8);
......
......@@ -55,7 +55,7 @@ public class ForkJoin3_SumArray_Limit_Join {
final ThreadLocalRandom random = ThreadLocalRandom.current();
System.out.println("Losowanie...");
for(int i = 0; i < tab.length; i++)
tab[i] = (byte)random.nextInt(256);
tab[i] = (byte)random.nextInt(128);
Long result;
final ForkJoinPool pool = new ForkJoinPool(8);
......
......@@ -42,7 +42,7 @@ public class ForkJoin4_SumArray_RecursiveTask {
final ThreadLocalRandom random = ThreadLocalRandom.current();
System.out.println("Losowanie...");
for(int i = 0; i < tab.length; i++)
tab[i] = (byte)random.nextInt(256);
tab[i] = (byte)random.nextInt(128);
Long result;
final ForkJoinPool pool = new ForkJoinPool(8);
......
......@@ -18,6 +18,11 @@ public class PuleWatkow_Lambda {
}
String s = futureString.get();
System.out.println("gotowe: " + s);
pool.shutdown();
// bez shutdown poniższy kod czekalby minutę, bo "może będą jeszcze jakieś zlecenia"
if(pool.awaitTermination(1, TimeUnit.MINUTES)) {
System.out.println("wszystko zrobione");
}
}
}
}
package na_zywo.lambdy;
public interface FunkcjaLiczbowa {
double oblicz(double arg);
default String hello() {
return "Hello!";
}
}
package na_zywo.lambdy;
public class Operacje {
public static void zmienTablice(double[] t, FunkcjaLiczbowa f) {
for(int i = 0; i < t.length; i++) {
t[i] = f.oblicz(t[i]);
}
}
}
package na_zywo.lambdy;
import java.util.Arrays;
public class Przyklady {
public static void main(String[] args) {
// "klasa anonimowa" dostępna od Java 1.1
FunkcjaLiczbowa a = new FunkcjaLiczbowa() {
public double oblicz(double arg) {
return 2*arg;
}
};
System.out.println(a.oblicz(100));
// Od Java 8 można używać "wyrażeń lambda"
FunkcjaLiczbowa f = x -> x*x;
System.out.println(f.oblicz(5));
FunkcjaLiczbowa silnia = (double x) -> {
double wynik = x;
for(int i = 1; i < x; i++) {
wynik *= i;
}
return wynik;
};
System.out.println(silnia.oblicz(5));
double[] liczby = {1, 0, 5, 9, 4, 1.25, 0.333};
System.out.println(Arrays.toString(liczby));
Operacje.zmienTablice(liczby, f);
System.out.println(Arrays.toString(liczby));
Operacje.zmienTablice(liczby, x -> Math.sqrt(x));
Operacje.zmienTablice(liczby, Math::sqrt);
System.out.println(Arrays.toString(liczby));
Operacje.zmienTablice(liczby, x -> 10*x);
System.out.println(Arrays.toString(liczby));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment