Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

181
Vistas
Concurrency Future syntax in Kotlin

I use simple java code for Future:

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

when I paste it to kotlin class it transforms to :

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

but when I try to get a value as in Java class I`m got null instead number

val integ = futureTask.get()

when I write return as in java code my ide warns that return is not allowed here.

Full kotlin code is next:

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() }

Output:

I'm Callable task.
null

What is a right syntax for Future?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

That is because there are two submit methods in ExecutorService and the wrong one is used.

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

Future<?> submit(Runnable task);

Your lambda function is translated to a Runnable instead of Callable. You can workaround this as following:

val futureTask = executor.submit(Callable {
    println("I'm Callable task.")
    1 + 1
})
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda