Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- abstract class TimeoutTask<T>(val countdown: Long) {
- private val thread = Thread({
- var result = doInBackground()
- if (!Thread.currentThread().isInterrupted()) {
- onComplete(result)
- Thread.currentThread().interrupt()
- }
- })
- private val timerTask = object: TimerTask() {
- override fun run() {
- if(!thread.isInterrupted()) {
- thread.interrupt()
- onInterrupt()
- }
- }
- }
- fun start() {
- thread.start()
- Timer().schedule(timerTask, countdown)
- }
- abstract fun onComplete(result: T)
- abstract fun doInBackground(): T
- abstract fun onInterrupt()
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement