Why CompletableFuture 's thenAccept() not running on the main thread
<p>I process the long running operation inside the CompletableFuture's supplyAsync() and get the result into thenAccept(). In some times thenAccept() perform on the main thread but some time it running on the worker thread.But I want run thenAccept() operation only on the main thread. this is the sample code.</p>

<pre><code>private void test() {

    ExecutorService executorService = Executors.newSingleThreadExecutor();

    CompletableFuture&lt;String&gt; cf1 = CompletableFuture.supplyAsync(() -&gt; {
        System.out.println("supplyAsync | I am running on : " + Thread.currentThread().getName());
        return "Hello world";
    }, executorService);

    CompletableFuture&lt;Void&gt; cf3 = cf1.thenAccept(s -&gt; {
        System.out.print("thenAccept | I am running on : " + Thread.currentThread().getName());
        System.out.println(" | answer : " + s);
    });

    cf3.thenRun(() -&gt; {
        System.out.println("thenRun | I am running on : " + Thread.currentThread().getName());
        System.out.println();
    });

}

public static void main(String[] args) {

    App app = new App();
    for(int i = 0; i &lt; 3; i++){
        app.test();
    }
}
</code></pre>

<p>result is :</p>

<pre><code>supplyAsync | I am running on : pool-1-thread-1
thenAccept | I am running on : main | answer : Hello world
thenRun | I am running on : main

supplyAsync | I am running on : pool-2-thread-1
thenAccept | I am running on : main | answer : Hello world
thenRun | I am running on : main

supplyAsync | I am running on : pool-3-thread-1
thenAccept | I am running on : pool-3-thread-1 | answer : Hello world
thenRun | I am running on : pool-3-thread-1
</code></pre>

<p>How can i fix this ?</p>