Java : Future.State with Examples
Future.State (Java SE 19 & JDK 19) API Examples.
You will find code examples on most Future.State methods.
Summary
Please see also : Future
try (final var executorService = Executors.newSingleThreadExecutor()) {
final var future = executorService.submit(() -> {
try {
System.out.println("-- task start --");
TimeUnit.SECONDS.sleep(2);
return "abcd";
} catch (InterruptedException e) {
System.out.println("Interrupted!");
return null;
} finally {
System.out.println("-- task end --");
}
});
TimeUnit.SECONDS.sleep(1);
System.out.println("state = " + future.state());
// Wait for the task to complete.
System.out.println("get = " + future.get());
System.out.println("state = " + future.state());
}
// Result
// ↓
//-- task start --
//state = RUNNING
//-- task end --
//get = abcd
//state = SUCCESS
Enum Constants
CANCELLED
try (final var executorService = Executors.newSingleThreadExecutor()) {
final var future = executorService.submit(() -> {
try {
System.out.println("-- task start --");
TimeUnit.SECONDS.sleep(2);
return "abcd";
} catch (InterruptedException e) {
System.out.println("Interrupted!");
return null;
} finally {
System.out.println("-- task end --");
}
});
TimeUnit.SECONDS.sleep(1);
System.out.println("state = " + future.state());
System.out.println("cancel = " + future.cancel(false));
System.out.println("state = " + future.state());
}
// Result
// ↓
//-- task start --
//state = RUNNING
//cancel = true
//state = CANCELLED
//-- task end --
FAILED
try (final var executorService = Executors.newSingleThreadExecutor()) {
final var future = executorService.submit(() -> {
try {
System.out.println("-- task start --");
TimeUnit.SECONDS.sleep(2);
throw new IllegalStateException("Fail!");
} catch (InterruptedException e) {
System.out.println("Interrupted!");
return null;
} finally {
System.out.println("-- task end --");
}
});
TimeUnit.SECONDS.sleep(1);
System.out.println("state = " + future.state());
try {
System.out.println("get = " + future.get());
} catch (ExecutionException e) {
System.out.println("ExecutionException! : " + e.getMessage());
}
System.out.println("state = " + future.state());
}
// Result
// ↓
//-- task start --
//state = RUNNING
//-- task end --
//ExecutionException! : java.lang.IllegalStateException: Fail!
//state = FAILED
RUNNING
try (final var executorService = Executors.newSingleThreadExecutor()) {
final var future = executorService.submit(() -> {
try {
System.out.println("-- task start --");
TimeUnit.SECONDS.sleep(2);
return "abcd";
} catch (InterruptedException e) {
System.out.println("Interrupted!");
return null;
} finally {
System.out.println("-- task end --");
}
});
TimeUnit.SECONDS.sleep(1);
System.out.println("state = " + future.state());
// Wait for the task to complete.
System.out.println("get = " + future.get());
System.out.println("state = " + future.state());
}
// Result
// ↓
//-- task start --
//state = RUNNING
//-- task end --
//get = abcd
//state = SUCCESS
SUCCESS
Please see RUNNING.
Methods
static Future.State valueOf (String name)
final var running = Future.State.valueOf("RUNNING");
System.out.println(running); // RUNNING
final var success = Future.State.valueOf("SUCCESS");
System.out.println(success); // SUCCESS
final var failed = Future.State.valueOf("FAILED");
System.out.println(failed); // FAILED
final var cancelled = Future.State.valueOf("CANCELLED");
System.out.println(cancelled); // CANCELLED
static Future.State[] values ()
for (final var value : Future.State.values()) {
System.out.println(value);
}
// Result
// ↓
//RUNNING
//SUCCESS
//FAILED
//CANCELLED
Methods declared in Enum
clone, compareTo, describeConstable, equals, finalize, getDeclaringClass, hashCode, name, ordinal, toString, valueOf
Please see the link below.
Related posts
- API Examples
- BlockingQueue
- Callable
- CancellationException
- ConcurrentHashMap.KeySetView
- ConcurrentLinkedDeque
- ConcurrentLinkedQueue
- ConcurrentMap
- ConcurrentModificationException
- ConcurrentSkipListSet
- Condition
- CopyOnWriteArrayList
- CopyOnWriteArraySet
- CountDownLatch
- CyclicBarrier
- Exchanger
- Executor
- ExecutorService
- Executors
- Future
- FutureTask
- InterruptedException
- Lock
- Runnable
- Semaphore
- Thread
- ThreadGroup
- ThreadLocal
- TimeUnit