Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

154
Views
¿Cómo forzar a CompletableFuture.thenApply() a ejecutarse en el mismo subproceso que ejecutó la etapa anterior?

Aquí hay una versión de código corto del problema al que me enfrento:

 public static void main(String[] args) { CompletableFuture.supplyAsync(() -> { /* try { Thread.sleep(2000); } catch (InterruptedException ignored) {} */ //System.out.println("supplyAsync: " + Thread.currentThread().getName()); return 1; }) .thenApply(i -> { System.out.println("apply: " + Thread.currentThread().getName()); return i + 1; }) .thenAccept((i) -> { System.out.println("accept: " + Thread.currentThread().getName()); System.out.println("result: " + i); }).join(); }

Esta es la salida que obtengo:

 apply: main accept: main result: 2

¡Me sorprende ver a main allí! Esperaba algo como esto que sucede cuando elimino el comentario de la llamada Thread.sleep() o incluso elimino el comentario de la única declaración sysout allí:

 supplyAsync: ForkJoinPool.commonPool-worker-1 apply: ForkJoinPool.commonPool-worker-1 accept: ForkJoinPool.commonPool-worker-1 result: 2

Entiendo que thenApplyAsync() se asegurará de que no se ejecute en el subproceso main , pero quiero evitar pasar los datos devueltos por el proveedor del subproceso que ejecutó supplyAsync al subproceso que se ejecutará thenApply y el otro subsiguiente then s en la cadena

over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!