provided a helper method to create a new TimerTask from a function block and added a simple test case for using timers
This commit is contained in:
@@ -5,37 +5,37 @@ import java.util.TimerTask
|
|||||||
import java.util.Date
|
import java.util.Date
|
||||||
|
|
||||||
public fun Timer.schedule(delay: Long, action: TimerTask.()->Unit) : TimerTask {
|
public fun Timer.schedule(delay: Long, action: TimerTask.()->Unit) : TimerTask {
|
||||||
val task = createTask(action)
|
val task = timerTask(action)
|
||||||
schedule(task, delay)
|
schedule(task, delay)
|
||||||
return task
|
return task
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun Timer.schedule(time: Date, action: TimerTask.()->Unit) : TimerTask {
|
public fun Timer.schedule(time: Date, action: TimerTask.()->Unit) : TimerTask {
|
||||||
val task = createTask(action)
|
val task = timerTask(action)
|
||||||
schedule(task, time)
|
schedule(task, time)
|
||||||
return task
|
return task
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun Timer.schedule(delay: Long, period: Long, action: TimerTask.()->Unit) : TimerTask {
|
public fun Timer.schedule(delay: Long, period: Long, action: TimerTask.()->Unit) : TimerTask {
|
||||||
val task = createTask(action)
|
val task = timerTask(action)
|
||||||
schedule(task, delay, period)
|
schedule(task, delay, period)
|
||||||
return task
|
return task
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun Timer.schedule(time: Date, period: Long, action: TimerTask.()->Unit) : TimerTask {
|
public fun Timer.schedule(time: Date, period: Long, action: TimerTask.()->Unit) : TimerTask {
|
||||||
val task = createTask(action)
|
val task = timerTask(action)
|
||||||
schedule(task, time, period)
|
schedule(task, time, period)
|
||||||
return task
|
return task
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun Timer.scheduleAtFixedRate(delay: Long, period: Long, action: TimerTask.()->Unit) : TimerTask {
|
public fun Timer.scheduleAtFixedRate(delay: Long, period: Long, action: TimerTask.()->Unit) : TimerTask {
|
||||||
val task = createTask(action)
|
val task = timerTask(action)
|
||||||
scheduleAtFixedRate(task, delay, period)
|
scheduleAtFixedRate(task, delay, period)
|
||||||
return task
|
return task
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun Timer.scheduleAtFixedRate(time: Date, period: Long, action: TimerTask.()->Unit) : TimerTask {
|
public fun Timer.scheduleAtFixedRate(time: Date, period: Long, action: TimerTask.()->Unit) : TimerTask {
|
||||||
val task = createTask(action)
|
val task = timerTask(action)
|
||||||
scheduleAtFixedRate(task, time, period)
|
scheduleAtFixedRate(task, time, period)
|
||||||
return task
|
return task
|
||||||
}
|
}
|
||||||
@@ -64,7 +64,7 @@ public fun fixedRateTimer(name: String? = null, daemon: Boolean = false, startAt
|
|||||||
return timer
|
return timer
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun createTask(action: TimerTask.()->Unit) : TimerTask = object: TimerTask() {
|
public fun timerTask(action: TimerTask.()->Unit) : TimerTask = object: TimerTask() {
|
||||||
public override fun run() {
|
public override fun run() {
|
||||||
action()
|
action()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,26 @@
|
|||||||
|
package concurrent
|
||||||
|
|
||||||
|
import kotlin.concurrent.*
|
||||||
|
import kotlin.test.*
|
||||||
|
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger
|
||||||
|
import java.util.Timer
|
||||||
|
|
||||||
|
import org.junit.Test as test
|
||||||
|
|
||||||
|
class TimerTest {
|
||||||
|
test fun scheduledTask() {
|
||||||
|
val counter = AtomicInteger(0)
|
||||||
|
val timer = Timer()
|
||||||
|
val task = timer.scheduleAtFixedRate(1000, 1000) {
|
||||||
|
val current = counter.incrementAndGet()
|
||||||
|
println("Tiemer fired at $current")
|
||||||
|
}
|
||||||
|
|
||||||
|
Thread.sleep(5000)
|
||||||
|
task.cancel()
|
||||||
|
|
||||||
|
val value = counter.get()
|
||||||
|
assertTrue(value > 2, "currnet counter is $value")
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user