Threads: using lambda expressiones instead of implementing Callable
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Thu, 5 Nov 2015 00:19:17 +0000 (01:19 +0100)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Thu, 5 Nov 2015 00:19:17 +0000 (01:19 +0100)
Allgemeines/Threads/Executor/ExecutorwithFuture/src/de/test/thread/executor/future/FutureTaskExample.java
Allgemeines/Threads/Executor/ExecutorwithFuture/src/de/test/thread/executor/future/TimerSchedulerExample.java

index a0ac154..9c168a7 100644 (file)
@@ -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<Car> task = new FutureTask<>(new Callable<Car>() {
-
-                       @Override
-                       public Car call() throws Exception {
-                               return new Car(99);
-                       }
-
-               });
+               FutureTask<Car> task = new FutureTask<>(() -> new Car(99));
+//             FutureTask<Car> task = new FutureTask<>(new Callable<Car>() {
+//
+//                     @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) {
index 3fbf1b6..ac420c6 100644 (file)
@@ -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<Integer> future = executor.schedule(new Callable<Integer>() {
-
-                       @Override
-                       public Integer call() throws Exception {
-                               return 666;
-                       }
-
-               }, 2000, TimeUnit.MILLISECONDS);
+               ScheduledFuture<Integer> future = executor.schedule(() -> 666, 2000, TimeUnit.MILLISECONDS);
+//             ScheduledFuture<Integer> future = executor.schedule(new Callable<Integer>() {
+//
+//                     @Override
+//                     public Integer call() throws Exception {
+//                             return 666;
+//                     }
+//
+//             }, 2000, TimeUnit.MILLISECONDS);
 
                long delay;
                while ((delay = future.getDelay(TimeUnit.MILLISECONDS)) > 0) {