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;
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();
}
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) {
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;
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) {