/
Timer.kt
148 lines (133 loc) · 6.08 KB
/
Timer.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
@file:JvmName("TimersKt")
package kotlin.concurrent
import java.util.Date
import java.util.Timer
import java.util.TimerTask
/**
* Schedules an [action] to be executed after the specified [delay] (expressed in milliseconds).
*/
@kotlin.internal.InlineOnly
public inline fun Timer.schedule(delay: Long, crossinline action: TimerTask.() -> Unit): TimerTask {
val task = timerTask(action)
schedule(task, delay)
return task
}
/**
* Schedules an [action] to be executed at the specified [time].
*/
@kotlin.internal.InlineOnly
public inline fun Timer.schedule(time: Date, crossinline action: TimerTask.() -> Unit): TimerTask {
val task = timerTask(action)
schedule(task, time)
return task
}
/**
* Schedules an [action] to be executed periodically, starting after the specified [delay] (expressed
* in milliseconds) and with the interval of [period] milliseconds between the end of the previous task
* and the start of the next one.
*/
@kotlin.internal.InlineOnly
public inline fun Timer.schedule(delay: Long, period: Long, crossinline action: TimerTask.() -> Unit): TimerTask {
val task = timerTask(action)
schedule(task, delay, period)
return task
}
/**
* Schedules an [action] to be executed periodically, starting at the specified [time] and with the
* interval of [period] milliseconds between the end of the previous task and the start of the next one.
*/
@kotlin.internal.InlineOnly
public inline fun Timer.schedule(time: Date, period: Long, crossinline action: TimerTask.() -> Unit): TimerTask {
val task = timerTask(action)
schedule(task, time, period)
return task
}
/**
* Schedules an [action] to be executed periodically, starting after the specified [delay] (expressed
* in milliseconds) and with the interval of [period] milliseconds between the start of the previous task
* and the start of the next one.
*/
@kotlin.internal.InlineOnly
public inline fun Timer.scheduleAtFixedRate(delay: Long, period: Long, crossinline action: TimerTask.() -> Unit): TimerTask {
val task = timerTask(action)
scheduleAtFixedRate(task, delay, period)
return task
}
/**
* Schedules an [action] to be executed periodically, starting at the specified [time] and with the
* interval of [period] milliseconds between the start of the previous task and the start of the next one.
*/
@kotlin.internal.InlineOnly
public inline fun Timer.scheduleAtFixedRate(time: Date, period: Long, crossinline action: TimerTask.() -> Unit): TimerTask {
val task = timerTask(action)
scheduleAtFixedRate(task, time, period)
return task
}
// exposed as public
@PublishedApi
internal fun timer(name: String?, daemon: Boolean): Timer = if (name == null) Timer(daemon) else Timer(name, daemon)
/**
* Creates a timer that executes the specified [action] periodically, starting after the specified [initialDelay]
* (expressed in milliseconds) and with the interval of [period] milliseconds between the end of the previous task
* and the start of the next one.
*
* @param name the name to use for the thread which is running the timer.
* @param daemon if `true`, the thread is started as a daemon thread (the VM will exit when only daemon threads are running).
*/
@kotlin.internal.InlineOnly
public inline fun timer(name: String? = null, daemon: Boolean = false, initialDelay: Long = 0.toLong(), period: Long, crossinline action: TimerTask.() -> Unit): Timer {
val timer = timer(name, daemon)
timer.schedule(initialDelay, period, action)
return timer
}
/**
* Creates a timer that executes the specified [action] periodically, starting at the specified [startAt] date
* and with the interval of [period] milliseconds between the end of the previous task and the start of the next one.
*
* @param name the name to use for the thread which is running the timer.
* @param daemon if `true`, the thread is started as a daemon thread (the VM will exit when only daemon threads are running).
*/
@kotlin.internal.InlineOnly
public inline fun timer(name: String? = null, daemon: Boolean = false, startAt: Date, period: Long, crossinline action: TimerTask.() -> Unit): Timer {
val timer = timer(name, daemon)
timer.schedule(startAt, period, action)
return timer
}
/**
* Creates a timer that executes the specified [action] periodically, starting after the specified [initialDelay]
* (expressed in milliseconds) and with the interval of [period] milliseconds between the start of the previous task
* and the start of the next one.
*
* @param name the name to use for the thread which is running the timer.
* @param daemon if `true`, the thread is started as a daemon thread (the VM will exit when only daemon threads are running).
*/
@kotlin.internal.InlineOnly
public inline fun fixedRateTimer(name: String? = null, daemon: Boolean = false, initialDelay: Long = 0.toLong(), period: Long, crossinline action: TimerTask.() -> Unit): Timer {
val timer = timer(name, daemon)
timer.scheduleAtFixedRate(initialDelay, period, action)
return timer
}
/**
* Creates a timer that executes the specified [action] periodically, starting at the specified [startAt] date
* and with the interval of [period] milliseconds between the start of the previous task and the start of the next one.
*
* @param name the name to use for the thread which is running the timer.
* @param daemon if `true`, the thread is started as a daemon thread (the VM will exit when only daemon threads are running).
*/
@kotlin.internal.InlineOnly
public inline fun fixedRateTimer(name: String? = null, daemon: Boolean = false, startAt: Date, period: Long, crossinline action: TimerTask.() -> Unit): Timer {
val timer = timer(name, daemon)
timer.scheduleAtFixedRate(startAt, period, action)
return timer
}
/**
* Wraps the specified [action] in a [TimerTask].
*/
@kotlin.internal.InlineOnly
public inline fun timerTask(crossinline action: TimerTask.() -> Unit): TimerTask = object : TimerTask() {
override fun run() = action()
}