Q:

Java executor model that allows running tasks only on instantiating thread

I am writing a multithreaded application where I've got the situation that I have certain library-related methods that can only be run on the main-thread. Currently I am running a simple, self-written, solution that listens on a queue for tasks and then executes them serially as they come in.
This works so far although it gets ugly when I need return values etc. (Which I am currently solving the c-way by passing along datastructures with the tasks and then have a busy-loop checking if the structure has been written to on the site of the method that queues a task.
This is not really the cleanest solution, and while it works, I was hoping to find a better way of doing this..
Q: Is there anything in the JDK or in a single specialized library that does what I need? Or is there any programming pattern that addresses my needs in  a clean and structured way?

A:

A possible solution with CompletableFuture:
class Job<T> {
    private final Supplier<T> computation;
    private final CompletableFuture<T> future;

    Job(Supplier<T> computation, CompletableFuture<T> future) {
        this.future = future;
        this.computation = computation;
    }

    public Supplier<T> getComputation() {
        return computation;
    }

    public CompletableFuture<T> getFuture() {
        return future;
    }
}

public void client() {
    // on the client:
    CompletableFuture<String> future = new CompletableFuture<>();
    Supplier<String> computation = () -> "Here I am!";

    enqueue(new Job<>(computation, future));

    String resultString = future.get();
}

public <T> void server(Job<T> job) {
    // on the server; job is taken from the queue
    CompletableFuture<T> future = job.getFuture();
    future.complete(job.getComputation().get());
}

Here, on the client, future.get() will wait infinitely until the result is available. There is also another form:
future.get(1, TimeUnit.MINUTES)

would only wait for one minute and then return. This for can be used for polling.

