Advertisement
Guest User

Untitled

a guest
Jul 3rd, 2015
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.71 KB | None | 0 0
  1. abstract class TimeoutTask<T>(val countdown: Long) {
  2.     private val thread = Thread({
  3.         var result = doInBackground()
  4.         if (!Thread.currentThread().isInterrupted()) {
  5.             onComplete(result)
  6.             Thread.currentThread().interrupt()
  7.         }
  8.     })
  9.  
  10.     private val timerTask = object: TimerTask() {
  11.         override fun run() {
  12.             if(!thread.isInterrupted()) {
  13.                 thread.interrupt()
  14.                 onInterrupt()
  15.             }
  16.         }
  17.     }
  18.  
  19.     fun start() {
  20.         thread.start()
  21.         Timer().schedule(timerTask, countdown)
  22.     }
  23.  
  24.     abstract fun onComplete(result: T)
  25.  
  26.     abstract fun doInBackground(): T
  27.  
  28.     abstract fun onInterrupt()
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement