Threads: futuretask and timerschedule examples
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Thu, 5 Nov 2015 00:12:38 +0000 (01:12 +0100)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Thu, 5 Nov 2015 00:12:38 +0000 (01:12 +0100)
Allgemeines/Threads/Executor/ExecutorwithFuture/src/de/test/thread/executor/future/FutureTaskExample.java [new file with mode: 0644]
Allgemeines/Threads/Executor/ExecutorwithFuture/src/de/test/thread/executor/future/TimerSchedulerExample.java [new file with mode: 0644]

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
new file mode 100644 (file)
index 0000000..a0ac154
--- /dev/null
@@ -0,0 +1,59 @@
+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;
+import java.util.concurrent.TimeoutException;
+
+
+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);
+                       }
+
+               });
+
+               new Thread(task).start();
+
+               try {
+                       carResult = task.get(1000, TimeUnit.MILLISECONDS);
+               } catch (InterruptedException e) {
+                       Thread.currentThread().interrupt();
+               } catch (ExecutionException e) {
+                       throw launderThrowable(e);
+               } catch (TimeoutException e) {
+                       System.out.println("Timeout");
+               } finally {
+                       task.cancel(true);
+               }
+
+               return carResult;
+       }
+
+       public static class Car {
+
+               final int id;
+
+               public Car(int id) {
+                       this.id = id;
+               }
+       }
+
+       private RuntimeException launderThrowable(final Throwable exception) {
+               exception.printStackTrace();
+               if (exception instanceof RuntimeException)
+                       return (RuntimeException)exception;
+               else if (exception instanceof Error)
+                       throw (Error)exception;
+               else
+                       throw new IllegalStateException("Not unchecked", 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
new file mode 100644 (file)
index 0000000..3fbf1b6
--- /dev/null
@@ -0,0 +1,51 @@
+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;
+import java.util.concurrent.ScheduledFuture;
+import java.util.concurrent.TimeUnit;
+
+
+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);
+
+               long delay;
+               while ((delay = future.getDelay(TimeUnit.MILLISECONDS)) > 0) {
+                       System.out.println("Delay: " + delay);
+               }
+
+               try {
+                       Integer result = future.get();
+                       System.out.println(result);
+               } catch (InterruptedException e) {
+                       Thread.currentThread().interrupt();
+               } catch (ExecutionException e) {
+                       throw launderThrowable(e);
+               } finally {
+                       future.cancel(true);
+               }
+       }
+       
+       
+    private RuntimeException launderThrowable(final Throwable exception) {
+        exception.printStackTrace();
+        if (exception instanceof RuntimeException)
+            return (RuntimeException) exception;
+        else if (exception instanceof Error)
+            throw (Error) exception;
+        else
+            throw new IllegalStateException("Not unchecked", exception);
+    }
+}