-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d2bf4b3
commit cb74b5b
Showing
2 changed files
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ class Main { | |
Delegates.main() | ||
Account.main(emptyArray()) | ||
IndexSolutionForRIDHashes.main() | ||
Timeouts.main() | ||
} | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
...lin-crums/jeorg-kotlin-crums-4-variance/src/main/kotlin/org/jesperancinha/ktd/Timeouts.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package org.jesperancinha.ktd | ||
|
||
import java.lang.Thread.sleep | ||
import java.util.* | ||
import kotlin.system.measureTimeMillis | ||
|
||
|
||
class Timeouts { | ||
|
||
internal class TimeOutTask(f: () -> Unit) : TimerTask() { | ||
|
||
val thread = Thread(f) | ||
override fun run() { | ||
thread.start() | ||
} | ||
|
||
override fun cancel(): Boolean { | ||
super.cancel() | ||
thread.interrupt() | ||
return true | ||
} | ||
} | ||
|
||
companion object { | ||
@JvmStatic | ||
fun main(args: Array<String> = emptyArray()) = measureTimeMillis { | ||
val timer = Timer() | ||
val scheduledFunction = { sleep(5000) } | ||
runFunction(timer = timer, timeout = 1000, f = scheduledFunction) | ||
}.let { | ||
println(it) | ||
} | ||
|
||
private fun runFunction(timer: Timer, timeout:Long, f: () -> Unit) { | ||
val timeOutTask = TimeOutTask(f) | ||
timer.schedule(timeOutTask, 0) | ||
sleep(timeout) | ||
timeOutTask.cancel() | ||
timer.cancel() | ||
} | ||
} | ||
} |