Java : Future.State - API使用例
Future.State (Java SE 19 & JDK 19) の使用例まとめです。
だいたいのメソッドを網羅済みです。
API仕様のおともにどうぞ。
概要
Future.State は、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());
// タスクが完了するのを待ちます。
System.out.println("get = " + future.get());
System.out.println("state = " + future.state());
}
// 結果
// ↓
//-- task start --
//state = RUNNING
//-- task end --
//get = abcd
//state = SUCCESS
列挙型定数
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());
}
// 結果
// ↓
//-- 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());
}
// 結果
// ↓
//-- 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());
// タスクが完了するのを待ちます。
System.out.println("get = " + future.get());
System.out.println("state = " + future.state());
}
// 結果
// ↓
//-- task start --
//state = RUNNING
//-- task end --
//get = abcd
//state = SUCCESS
SUCCESS
このメソッドの使用例は、RUNNING にまとめて記載しました。
そちらのAPI使用例をご参照ください。
メソッド
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);
}
// 結果
// ↓
//RUNNING
//SUCCESS
//FAILED
//CANCELLED
Enumで宣言されたメソッド
clone, compareTo, describeConstable, equals, finalize, getDeclaringClass, hashCode, name, ordinal, toString, valueOf
「Java API 使用例 : Enum」をご参照ください。
関連記事
- API 使用例
- BlockingQueue (ブロッキング・キュー)
- Callable
- CancellationException
- ConcurrentHashMap.KeySetView (並列処理用セット)
- ConcurrentLinkedDeque (並列処理用・両端キュー)
- ConcurrentLinkedQueue (並列処理用キュー)
- ConcurrentMap (並列処理用マップ)
- ConcurrentModificationException (並列処理例外)
- ConcurrentSkipListSet (並列処理用セット)
- Condition (同期)
- CopyOnWriteArrayList (並列処理用リスト)
- CopyOnWriteArraySet (並列処理用セット)
- CountDownLatch (同期)
- CyclicBarrier (同期)
- Exchanger (同期)
- Executor
- ExecutorService
- Executors
- Future
- FutureTask
- InterruptedException (割込み例外)
- Lock (同期)
- Object (オブジェクト)
- Runnable
- Semaphore (セマフォ)
- Thread (スレッド)
- ThreadGroup
- ThreadLocal
- TimeUnit