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

180
Views
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 answers
Answer question

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 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!