February 2018
Intermediate to advanced
350 pages
7h 35m
English
As callback style tends to be hard to maintain, other styles have emerged in recent years. One of these styles is futures. A future is a computation that may complete in the future. When we invoke the Future.get method, it will obtain its result, but we also block the thread:
import java.util.concurrent.ExecutorServiceimport java.util.concurrent.Executorsclass FutureUserService(private val userClient: UserClient, private val factClient: FactClient, private val userRepository: UserRepository, private val factRepository: FactRepository) : UserService { override fun getFact(id: UserId): Fact { val executor = Executors.newFixedThreadPool(2) val user = executor.submit<User?> { userRepository.getUserById(id) }.get() return if (user ...
Read now
Unlock full access