From 449ded734e48ebc54d8fb0bc3d03cafaa5d9006a Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Thu, 5 Nov 2015 01:12:38 +0100 Subject: [PATCH] Threads: futuretask and timerschedule examples --- .../thread/executor/future/FutureTaskExample.java | 59 ++++++++++++++++++++++ .../executor/future/TimerSchedulerExample.java | 51 +++++++++++++++++++ 2 files changed, 110 insertions(+) create mode 100644 Allgemeines/Threads/Executor/ExecutorwithFuture/src/de/test/thread/executor/future/FutureTaskExample.java create mode 100644 Allgemeines/Threads/Executor/ExecutorwithFuture/src/de/test/thread/executor/future/TimerSchedulerExample.java 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 index 0000000..a0ac154 --- /dev/null +++ b/Allgemeines/Threads/Executor/ExecutorwithFuture/src/de/test/thread/executor/future/FutureTaskExample.java @@ -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 task = new FutureTask<>(new Callable() { + + @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 index 0000000..3fbf1b6 --- /dev/null +++ b/Allgemeines/Threads/Executor/ExecutorwithFuture/src/de/test/thread/executor/future/TimerSchedulerExample.java @@ -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 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) { + 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); + } +} -- 2.1.4