From: Gustavo Martin Morcuende Date: Thu, 5 Nov 2015 00:19:17 +0000 (+0100) Subject: Threads: using lambda expressiones instead of implementing Callable X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=fe74a5611fe8fbcea6140896415a18fa157b5b9b;p=JavaForFun Threads: using lambda expressiones instead of implementing Callable --- diff --git a/Allgemeines/Threads/Executor/ExecutorwithFuture/src/de/test/thread/executor/future/FutureTaskExample.java b/Allgemeines/Threads/Executor/ExecutorwithFuture/src/de/test/thread/executor/future/FutureTaskExample.java index a0ac154..9c168a7 100644 --- a/Allgemeines/Threads/Executor/ExecutorwithFuture/src/de/test/thread/executor/future/FutureTaskExample.java +++ b/Allgemeines/Threads/Executor/ExecutorwithFuture/src/de/test/thread/executor/future/FutureTaskExample.java @@ -1,6 +1,5 @@ package de.test.thread.executor.future; -import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.FutureTask; import java.util.concurrent.TimeUnit; @@ -11,14 +10,15 @@ public class FutureTaskExample { public Car test() { Car carResult = null; - FutureTask task = new FutureTask<>(new Callable() { - - @Override - public Car call() throws Exception { - return new Car(99); - } - - }); + FutureTask task = new FutureTask<>(() -> new Car(99)); +// FutureTask task = new FutureTask<>(new Callable() { +// +// @Override +// public Car call() throws Exception { +// return new Car(99); +// } +// +// }); new Thread(task).start(); @@ -38,12 +38,15 @@ public class FutureTaskExample { } public static class Car { - - final int id; + private final int id; public Car(int id) { this.id = id; } + + public int getId() { + return id; + } } private RuntimeException launderThrowable(final Throwable exception) { diff --git a/Allgemeines/Threads/Executor/ExecutorwithFuture/src/de/test/thread/executor/future/TimerSchedulerExample.java b/Allgemeines/Threads/Executor/ExecutorwithFuture/src/de/test/thread/executor/future/TimerSchedulerExample.java index 3fbf1b6..ac420c6 100644 --- a/Allgemeines/Threads/Executor/ExecutorwithFuture/src/de/test/thread/executor/future/TimerSchedulerExample.java +++ b/Allgemeines/Threads/Executor/ExecutorwithFuture/src/de/test/thread/executor/future/TimerSchedulerExample.java @@ -1,6 +1,5 @@ package de.test.thread.executor.future; -import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; @@ -12,14 +11,15 @@ public class TimerSchedulerExample { ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(); public void test() { - ScheduledFuture future = executor.schedule(new Callable() { - - @Override - public Integer call() throws Exception { - return 666; - } - - }, 2000, TimeUnit.MILLISECONDS); + ScheduledFuture future = executor.schedule(() -> 666, 2000, TimeUnit.MILLISECONDS); +// ScheduledFuture future = executor.schedule(new Callable() { +// +// @Override +// public Integer call() throws Exception { +// return 666; +// } +// +// }, 2000, TimeUnit.MILLISECONDS); long delay; while ((delay = future.getDelay(TimeUnit.MILLISECONDS)) > 0) {