Advertisement
Guest User

Untitled

a guest
Mar 26th, 2016
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. import java.util.concurrent.CompletableFuture
  2. import java.util.concurrent.Executors
  3.  
  4. /**
  5. * Project: FuturesDemo
  6. * Created by jdavid on 24.03.16.
  7. */
  8.  
  9. val executor = Executors.newCachedThreadPool()
  10.  
  11. data class LoginResponse(val success: Boolean)
  12.  
  13. interface ILoginService {
  14. fun login(username: String, password: String): CompletableFuture<LoginResponse>
  15. }
  16.  
  17. class DummyLoginService : ILoginService {
  18. override fun login(username: String, password: String): CompletableFuture<LoginResponse> {
  19. val response = CompletableFuture<LoginResponse>()
  20.  
  21. executor.submit {
  22. Thread.sleep(2000)
  23. response.complete(LoginResponse(true))
  24. }
  25.  
  26. return response
  27. }
  28. }
  29.  
  30. object FutureDemo {
  31.  
  32. @JvmStatic
  33. fun main(args: Array<String>) {
  34. val loginService: ILoginService = DummyLoginService()
  35.  
  36. loginService.login("toto", "swordfish").whenComplete { response, throwable ->
  37. if (response.success) {
  38. println("Go to main menu")
  39. }
  40. }
  41. println("Login attempted")
  42. }
  43.  
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement