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

186
Views
Sintaxis futura de concurrencia en Kotlin

Yo uso código java simple para Future:

 Future<Integer> futureTask = executor.submit(() -> { System.out.println("I'm Callable task."); return 1 + 1; });

cuando lo pego en la clase kotlin, se transforma en:

 val futureTask = executor.submit { println("I'm Callable task.") 1 + 1 }

pero cuando trato de obtener un valor como en la clase Java, obtengo un número nulo en lugar de un número

 val integ = futureTask.get()

cuando escribo return como en el código Java, mi ide advierte que aquí no se permite el retorno.

El código completo de Kotlin es el siguiente:

 fun main(args: Array<String>) { val executor = Executors.newSingleThreadExecutor() val futureTask = executor.submit { println("I'm Callable task.") 1 + 1 } println(futureTask.get()) executor.shutdown() }

Producción:

 I'm Callable task. null

¿Cuál es una sintaxis correcta para Future ?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Esto se debe a que hay dos métodos de submit en ExecutorService y se usa el incorrecto.

 <T> Future<T> submit(Callable<T> task); Future<?> submit(Runnable task);

Su función lambda se traduce a Runnable en lugar de Callable. Puede solucionar esto de la siguiente manera:

 val futureTask = executor.submit(Callable { println("I'm Callable task.") 1 + 1 })
over 4 years ago · Santiago Trujillo Report
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!