Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.concurrent.CompletableFuture
- import java.util.concurrent.Executors
- /**
- * Project: FuturesDemo
- * Created by jdavid on 24.03.16.
- */
- val executor = Executors.newCachedThreadPool()
- data class LoginResponse(val success: Boolean)
- interface ILoginService {
- fun login(username: String, password: String): CompletableFuture<LoginResponse>
- }
- class DummyLoginService : ILoginService {
- override fun login(username: String, password: String): CompletableFuture<LoginResponse> {
- val response = CompletableFuture<LoginResponse>()
- executor.submit {
- Thread.sleep(2000)
- response.complete(LoginResponse(true))
- }
- return response
- }
- }
- object FutureDemo {
- @JvmStatic
- fun main(args: Array<String>) {
- val loginService: ILoginService = DummyLoginService()
- loginService.login("toto", "swordfish").whenComplete { response, throwable ->
- if (response.success) {
- println("Go to main menu")
- }
- }
- println("Login attempted")
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement