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

Represents the computation state.

Class diagram

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

The task was 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

The task completed with an exception.

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

The task has not completed.

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

The task completed with a result.

Please see RUNNING.

Methods

static Future.State valueOf (String name)

Returns the enum constant of this class with the specified 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 ()

Returns an array containing the constants of this enum class, in the order they are declared.

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

To top of page