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
77import java.util.concurrent.locks.LockSupport
88
99internal interface TimeSource {
10+ fun currentTimeMillis (): Long
1011 fun nanoTime (): Long
1112 fun trackTask (block : Runnable ): Runnable
1213 fun unTrackTask ()
@@ -17,6 +18,7 @@ internal interface TimeSource {
1718}
1819
1920internal object DefaultTimeSource : TimeSource {
21+ override fun currentTimeMillis (): Long = System .currentTimeMillis()
2022 override fun nanoTime (): Long = System .nanoTime()
2123 override fun trackTask (block : Runnable ): Runnable = block
2224 override fun unTrackTask () {}
Original file line number Diff line number Diff line change @@ -8,13 +8,13 @@ package kotlinx.coroutines.experimental.guide.cancel02
88import kotlinx.coroutines.experimental.*
99
1010fun main (args : Array <String >) = runBlocking<Unit > {
11- val startTime = System .currentTimeMillis()
11+ val startTime = timeSource .currentTimeMillis()
1212 val job = launch {
1313 var nextPrintTime = startTime
1414 var i = 0
1515 while (i < 5 ) { // computation loop, just wastes CPU
1616 // print a message twice a second
17- if (System .currentTimeMillis() >= nextPrintTime) {
17+ if (timeSource .currentTimeMillis() >= nextPrintTime) {
1818 println (" I'm sleeping ${i++ } ..." )
1919 nextPrintTime + = 500L
2020 }
Original file line number Diff line number Diff line change @@ -8,13 +8,13 @@ package kotlinx.coroutines.experimental.guide.cancel03
88import kotlinx.coroutines.experimental.*
99
1010fun main (args : Array <String >) = runBlocking<Unit > {
11- val startTime = System .currentTimeMillis()
11+ val startTime = timeSource .currentTimeMillis()
1212 val job = launch {
1313 var nextPrintTime = startTime
1414 var i = 0
1515 while (isActive) { // cancellable computation loop
1616 // print a message twice a second
17- if (System .currentTimeMillis() >= nextPrintTime) {
17+ if (timeSource .currentTimeMillis() >= nextPrintTime) {
1818 println (" I'm sleeping ${i++ } ..." )
1919 nextPrintTime + = 500L
2020 }
Original file line number Diff line number Diff line change @@ -11,9 +11,9 @@ import kotlinx.coroutines.experimental.channels.*
1111fun main (args : Array <String >) = runBlocking<Unit > {
1212 val tickerChannel = ticker(delay = 100 , initialDelay = 0 ) // create ticker channel
1313 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
1515
16- nextElement = withTimeoutOrNull(50 ) { tickerChannel.receive() } // All subsequent elements has 100ms delay
16+ nextElement = withTimeoutOrNull(50 ) { tickerChannel.receive() } // all subsequent elements has 100ms delay
1717 println (" Next element is not ready in 50 ms: $nextElement " )
1818
1919 nextElement = withTimeoutOrNull(60 ) { tickerChannel.receive() }
Original file line number Diff line number Diff line change @@ -130,6 +130,7 @@ private class TestTimeSource(
130130
131131 private val threads = ConcurrentHashMap <Thread , ThreadStatus >()
132132
133+ override fun currentTimeMillis (): Long = TimeUnit .NANOSECONDS .toMillis(time)
133134 override fun nanoTime (): Long = time
134135
135136 @Synchronized
Original file line number Diff line number Diff line change @@ -214,7 +214,9 @@ fun knit(markdownFileName: String): Boolean {
214214 outLines + = line
215215 }
216216 }
217- outLines + = codeLines
217+ for (code in codeLines) {
218+ outLines + = code.replace(" System.currentTimeMillis()" , " timeSource.currentTimeMillis()" )
219+ }
218220 codeLines.clear()
219221 writeLinesIfNeeded(file, outLines)
220222 }
Original file line number Diff line number Diff line change @@ -687,7 +687,6 @@ After delay
687687[ Job.cancel ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-job/cancel.html
688688[ withContext ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/with-context.html
689689[ 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
691690[ CoroutineStart ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-coroutine-start/index.html
692691[ async ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/async.html
693692[ 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