From fe74a5611fe8fbcea6140896415a18fa157b5b9b Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Thu, 5 Nov 2015 01:19:17 +0100 Subject: [PATCH] Threads: using lambda expressiones instead of implementing Callable --- .../thread/executor/future/FutureTaskExample.java | 25 ++++++++++++---------- .../executor/future/TimerSchedulerExample.java | 18 ++++++++-------- 2 files changed, 23 insertions(+), 20 deletions(-) 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) { -- 2.1.4