File tree Expand file tree Collapse file tree 7 files changed +12
-8
lines changed
core/kotlinx-coroutines-core Expand file tree Collapse file tree 7 files changed +12
-8
lines changed Original file line number Diff line number Diff line change @@ -7,6 +7,7 @@ package kotlinx.coroutines.experimental
7
7
import java.util.concurrent.locks.LockSupport
8
8
9
9
internal interface TimeSource {
10
+ fun currentTimeMillis (): Long
10
11
fun nanoTime (): Long
11
12
fun trackTask (block : Runnable ): Runnable
12
13
fun unTrackTask ()
@@ -17,6 +18,7 @@ internal interface TimeSource {
17
18
}
18
19
19
20
internal object DefaultTimeSource : TimeSource {
21
+ override fun currentTimeMillis (): Long = System .currentTimeMillis()
20
22
override fun nanoTime (): Long = System .nanoTime()
21
23
override fun trackTask (block : Runnable ): Runnable = block
22
24
override fun unTrackTask () {}
Original file line number Diff line number Diff line change @@ -8,13 +8,13 @@ package kotlinx.coroutines.experimental.guide.cancel02
8
8
import kotlinx.coroutines.experimental.*
9
9
10
10
fun main (args : Array <String >) = runBlocking<Unit > {
11
- val startTime = System .currentTimeMillis()
11
+ val startTime = timeSource .currentTimeMillis()
12
12
val job = launch {
13
13
var nextPrintTime = startTime
14
14
var i = 0
15
15
while (i < 5 ) { // computation loop, just wastes CPU
16
16
// print a message twice a second
17
- if (System .currentTimeMillis() >= nextPrintTime) {
17
+ if (timeSource .currentTimeMillis() >= nextPrintTime) {
18
18
println (" I'm sleeping ${i++ } ..." )
19
19
nextPrintTime + = 500L
20
20
}
Original file line number Diff line number Diff line change @@ -8,13 +8,13 @@ package kotlinx.coroutines.experimental.guide.cancel03
8
8
import kotlinx.coroutines.experimental.*
9
9
10
10
fun main (args : Array <String >) = runBlocking<Unit > {
11
- val startTime = System .currentTimeMillis()
11
+ val startTime = timeSource .currentTimeMillis()
12
12
val job = launch {
13
13
var nextPrintTime = startTime
14
14
var i = 0
15
15
while (isActive) { // cancellable computation loop
16
16
// print a message twice a second
17
- if (System .currentTimeMillis() >= nextPrintTime) {
17
+ if (timeSource .currentTimeMillis() >= nextPrintTime) {
18
18
println (" I'm sleeping ${i++ } ..." )
19
19
nextPrintTime + = 500L
20
20
}
Original file line number Diff line number Diff line change @@ -11,9 +11,9 @@ import kotlinx.coroutines.experimental.channels.*
11
11
fun main (args : Array <String >) = runBlocking<Unit > {
12
12
val tickerChannel = ticker(delay = 100 , initialDelay = 0 ) // create ticker channel
13
13
var nextElement = withTimeoutOrNull(1 ) { tickerChannel.receive() }
14
- println (" Initial element is available immediately: $nextElement " ) // Initial delay hasn't passed yet
14
+ println (" Initial element is available immediately: $nextElement " ) // initial delay hasn't passed yet
15
15
16
- nextElement = withTimeoutOrNull(50 ) { tickerChannel.receive() } // All subsequent elements has 100ms delay
16
+ nextElement = withTimeoutOrNull(50 ) { tickerChannel.receive() } // all subsequent elements has 100ms delay
17
17
println (" Next element is not ready in 50 ms: $nextElement " )
18
18
19
19
nextElement = withTimeoutOrNull(60 ) { tickerChannel.receive() }
Original file line number Diff line number Diff line change @@ -130,6 +130,7 @@ private class TestTimeSource(
130
130
131
131
private val threads = ConcurrentHashMap <Thread , ThreadStatus >()
132
132
133
+ override fun currentTimeMillis (): Long = TimeUnit .NANOSECONDS .toMillis(time)
133
134
override fun nanoTime (): Long = time
134
135
135
136
@Synchronized
Original file line number Diff line number Diff line change @@ -214,7 +214,9 @@ fun knit(markdownFileName: String): Boolean {
214
214
outLines + = line
215
215
}
216
216
}
217
- outLines + = codeLines
217
+ for (code in codeLines) {
218
+ outLines + = code.replace(" System.currentTimeMillis()" , " timeSource.currentTimeMillis()" )
219
+ }
218
220
codeLines.clear()
219
221
writeLinesIfNeeded(file, outLines)
220
222
}
Original file line number Diff line number Diff line change @@ -687,7 +687,6 @@ After delay
687
687
[ Job.cancel ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-job/cancel.html
688
688
[ withContext ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/with-context.html
689
689
[ CommonPool ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-common-pool/index.html
690
- [ NonCancellable ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-non-cancellable/index.html
691
690
[ CoroutineStart ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-coroutine-start/index.html
692
691
[ async ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/async.html
693
692
[ CoroutineStart.UNDISPATCHED ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-coroutine-start/-u-n-d-i-s-p-a-t-c-h-e-d.html
You can’t perform that action at this time.
0 commit comments