Future.get no puede bloquear el hilo en forkjoinpool / workstealingpool

cero:

Me puse el tamaño de workstealingpool como 1. Parece que el future.get () no bloquea el hilo.

@Test
public void runAsyncThenApplyExample() {
    ExecutorService executor = Executors.newWorkStealingPool(1);
    CompletableFuture cf = CompletableFuture.supplyAsync(
            () -> {
                //assertTrue(Thread.currentThread().isDaemon());
                System.out.println("func: " + threadName());
                Callable<Long> callable = () ->stub();
                Future<Long> future = executor.submit(callable);
                try {
                    future.get();  <<<<< **I think this should block the thread**
                } catch (InterruptedException | ExecutionException e) {
                    e.printStackTrace();
                }
                return 1;
            }, executor).thenAccept(s -> {
        System.out.println("accept: " + threadName());
    });
    //assertFalse(cf.isDone());
    System.out.println("main: " + threadName());
    sleep(10);
    assertTrue(cf.isDone());
}

private Long stub() {
    System.out.println("stub: " + threadName());
    return 1L;
}
private String threadName() {
    return Thread.currentThread().getName();
}

salida:

func: ForkJoinPool-1-trabajador-3
: Los principales
stub: ForkJoinPool-1-trabajador-3
aceptar: ForkJoinPool-1-trabajador-3

Parece que Future.get () y talón están utilizando el mismo threead. introducir descripción de la imagen aquí

introducir descripción de la imagen aquí

Oleg:

Executors.newWorkStealingPool(1);usos ForkJoinPoolque tiene una característica no documentada llamada hilos de compensación.

De http://www.coopsoft.com/ar/CalamityArticle.html (el énfasis es mío):

Introducido con JDK1.8 es la clase CompletableFuture. Para citar el JavaDoc:

“A Future that may be explicitly completed (setting its value and status), and may include dependent functions and actions that trigger upon its completion.”

Not mentioned in the JavaDoc is that when using a large number of dependent functions with a get() method, the framework creates “compensation threads” to continue fetching application tasks from the deques and submission queue.

So when you do future.get(); it blocks but another thread is created in which the task is executed.

When running your code the output I'm getting is:

func: ForkJoinPool-1-worker-1
main: main
stub: ForkJoinPool-1-worker-0
accept: ForkJoinPool-1-worker-1

No ha mostrado su threadName()método tal vez hay un error en él y debido a que está viendo el mismo nombre hilo (o utilizar diferentes JVM que utiliza el mismo nombre en tal caso, compruebe el ID del hilo)? Si no por favor proporcionar el código completo que da salida FUNC y talón como los mismos hilos.

Supongo que te gusta

Origin http://43.154.161.224:23101/article/api/json?id=214688&siteId=1
Recomendado
Clasificación