File tree Expand file tree Collapse file tree 10 files changed +205
-244
lines changed
kotlinx-coroutines-core/jvm/test/guide Expand file tree Collapse file tree 10 files changed +205
-244
lines changed Load Diff Large diffs are not rendered by default.
Original file line number Diff line number Diff line change @@ -8,27 +8,30 @@ package kotlinx.coroutines.guide.sync01
8
8
import kotlinx.coroutines.*
9
9
import kotlin.system.*
10
10
11
- suspend fun CoroutineScope. massiveRun (action : suspend () -> Unit ) {
11
+ suspend fun massiveRun (action : suspend () -> Unit ) {
12
12
val n = 100 // number of coroutines to launch
13
13
val k = 1000 // times an action is repeated by each coroutine
14
14
val time = measureTimeMillis {
15
- val jobs = List (n) {
16
- launch {
17
- repeat(k) { action() }
15
+ coroutineScope { // scope for coroutines
16
+ repeat(n) {
17
+ launch {
18
+ repeat(k) { action() }
19
+ }
18
20
}
19
21
}
20
- jobs.forEach { it.join() }
21
22
}
22
23
println (" Completed ${n * k} actions in $time ms" )
23
24
}
24
25
26
+ // sampleStart
25
27
var counter = 0
26
28
27
- fun main () = runBlocking<Unit > {
28
- // sampleStart
29
- GlobalScope .massiveRun {
30
- counter++
29
+ fun main () = runBlocking {
30
+ withContext(Dispatchers .Default ) {
31
+ massiveRun {
32
+ counter++
33
+ }
31
34
}
32
35
println (" Counter = $counter " )
33
- // sampleEnd
34
36
}
37
+ // sampleEnd
Load Diff This file was deleted.
Original file line number Diff line number Diff line change @@ -8,26 +8,31 @@ package kotlinx.coroutines.guide.sync02
8
8
import kotlinx.coroutines.*
9
9
import kotlin.system.*
10
10
11
- suspend fun CoroutineScope. massiveRun (action : suspend () -> Unit ) {
11
+ suspend fun massiveRun (action : suspend () -> Unit ) {
12
12
val n = 100 // number of coroutines to launch
13
13
val k = 1000 // times an action is repeated by each coroutine
14
14
val time = measureTimeMillis {
15
- val jobs = List (n) {
16
- launch {
17
- repeat(k) { action() }
15
+ coroutineScope { // scope for coroutines
16
+ repeat(n) {
17
+ launch {
18
+ repeat(k) { action() }
19
+ }
18
20
}
19
21
}
20
- jobs.forEach { it.join() }
21
22
}
22
23
println (" Completed ${n * k} actions in $time ms" )
23
24
}
24
25
26
+ // sampleStart
25
27
@Volatile // in Kotlin `volatile` is an annotation
26
28
var counter = 0
27
29
28
- fun main () = runBlocking<Unit > {
29
- GlobalScope .massiveRun {
30
- counter++
30
+ fun main () = runBlocking {
31
+ withContext(Dispatchers .Default ) {
32
+ massiveRun {
33
+ counter++
34
+ }
31
35
}
32
36
println (" Counter = $counter " )
33
37
}
38
+ // sampleEnd
Original file line number Diff line number Diff line change @@ -9,27 +9,30 @@ import kotlinx.coroutines.*
9
9
import java.util.concurrent.atomic.*
10
10
import kotlin.system.*
11
11
12
- suspend fun CoroutineScope. massiveRun (action : suspend () -> Unit ) {
12
+ suspend fun massiveRun (action : suspend () -> Unit ) {
13
13
val n = 100 // number of coroutines to launch
14
14
val k = 1000 // times an action is repeated by each coroutine
15
15
val time = measureTimeMillis {
16
- val jobs = List (n) {
17
- launch {
18
- repeat(k) { action() }
16
+ coroutineScope { // scope for coroutines
17
+ repeat(n) {
18
+ launch {
19
+ repeat(k) { action() }
20
+ }
19
21
}
20
22
}
21
- jobs.forEach { it.join() }
22
23
}
23
24
println (" Completed ${n * k} actions in $time ms" )
24
25
}
25
26
27
+ // sampleStart
26
28
var counter = AtomicInteger ()
27
29
28
- fun main () = runBlocking<Unit > {
29
- // sampleStart
30
- GlobalScope .massiveRun {
31
- counter.incrementAndGet()
30
+ fun main () = runBlocking {
31
+ withContext(Dispatchers .Default ) {
32
+ massiveRun {
33
+ counter.incrementAndGet()
34
+ }
32
35
}
33
- println (" Counter = ${counter.get()} " )
34
- // sampleEnd
36
+ println (" Counter = $counter " )
35
37
}
38
+ // sampleEnd
Original file line number Diff line number Diff line change @@ -8,30 +8,34 @@ package kotlinx.coroutines.guide.sync04
8
8
import kotlinx.coroutines.*
9
9
import kotlin.system.*
10
10
11
- suspend fun CoroutineScope. massiveRun (action : suspend () -> Unit ) {
11
+ suspend fun massiveRun (action : suspend () -> Unit ) {
12
12
val n = 100 // number of coroutines to launch
13
13
val k = 1000 // times an action is repeated by each coroutine
14
14
val time = measureTimeMillis {
15
- val jobs = List (n) {
16
- launch {
17
- repeat(k) { action() }
15
+ coroutineScope { // scope for coroutines
16
+ repeat(n) {
17
+ launch {
18
+ repeat(k) { action() }
19
+ }
18
20
}
19
21
}
20
- jobs.forEach { it.join() }
21
22
}
22
23
println (" Completed ${n * k} actions in $time ms" )
23
24
}
24
25
26
+ // sampleStart
25
27
val counterContext = newSingleThreadContext(" CounterContext" )
26
28
var counter = 0
27
29
28
- fun main () = runBlocking<Unit > {
29
- // sampleStart
30
- GlobalScope .massiveRun { // run each coroutine with DefaultDispathcer
31
- withContext(counterContext) { // but confine each increment to the single-threaded context
32
- counter++
30
+ fun main () = runBlocking {
31
+ withContext(Dispatchers .Default ) {
32
+ massiveRun {
33
+ // confine each increment to a single-threaded context
34
+ withContext(counterContext) {
35
+ counter++
36
+ }
33
37
}
34
38
}
35
39
println (" Counter = $counter " )
36
- // sampleEnd
37
40
}
41
+ // sampleEnd
Original file line number Diff line number Diff line change @@ -8,28 +8,32 @@ package kotlinx.coroutines.guide.sync05
8
8
import kotlinx.coroutines.*
9
9
import kotlin.system.*
10
10
11
- suspend fun CoroutineScope. massiveRun (action : suspend () -> Unit ) {
11
+ suspend fun massiveRun (action : suspend () -> Unit ) {
12
12
val n = 100 // number of coroutines to launch
13
13
val k = 1000 // times an action is repeated by each coroutine
14
14
val time = measureTimeMillis {
15
- val jobs = List (n) {
16
- launch {
17
- repeat(k) { action() }
15
+ coroutineScope { // scope for coroutines
16
+ repeat(n) {
17
+ launch {
18
+ repeat(k) { action() }
19
+ }
18
20
}
19
21
}
20
- jobs.forEach { it.join() }
21
22
}
22
23
println (" Completed ${n * k} actions in $time ms" )
23
24
}
24
25
26
+ // sampleStart
25
27
val counterContext = newSingleThreadContext(" CounterContext" )
26
28
var counter = 0
27
29
28
- fun main () = runBlocking<Unit > {
29
- // sampleStart
30
- CoroutineScope (counterContext).massiveRun { // run each coroutine in the single-threaded context
31
- counter++
30
+ fun main () = runBlocking {
31
+ // confine everything to a single-threaded context
32
+ withContext(counterContext) {
33
+ massiveRun {
34
+ counter++
35
+ }
32
36
}
33
37
println (" Counter = $counter " )
34
- // sampleEnd
35
38
}
39
+ // sampleEnd
Original file line number Diff line number Diff line change @@ -9,30 +9,34 @@ import kotlinx.coroutines.*
9
9
import kotlinx.coroutines.sync.*
10
10
import kotlin.system.*
11
11
12
- suspend fun CoroutineScope. massiveRun (action : suspend () -> Unit ) {
12
+ suspend fun massiveRun (action : suspend () -> Unit ) {
13
13
val n = 100 // number of coroutines to launch
14
14
val k = 1000 // times an action is repeated by each coroutine
15
15
val time = measureTimeMillis {
16
- val jobs = List (n) {
17
- launch {
18
- repeat(k) { action() }
16
+ coroutineScope { // scope for coroutines
17
+ repeat(n) {
18
+ launch {
19
+ repeat(k) { action() }
20
+ }
19
21
}
20
22
}
21
- jobs.forEach { it.join() }
22
23
}
23
24
println (" Completed ${n * k} actions in $time ms" )
24
25
}
25
26
27
+ // sampleStart
26
28
val mutex = Mutex ()
27
29
var counter = 0
28
30
29
- fun main () = runBlocking<Unit > {
30
- // sampleStart
31
- GlobalScope .massiveRun {
32
- mutex.withLock {
33
- counter++
31
+ fun main () = runBlocking {
32
+ withContext(Dispatchers .Default ) {
33
+ massiveRun {
34
+ // protect each increment with lock
35
+ mutex.withLock {
36
+ counter++
37
+ }
34
38
}
35
39
}
36
40
println (" Counter = $counter " )
37
- // sampleEnd
38
41
}
42
+ // sampleEnd
Original file line number Diff line number Diff line change @@ -9,16 +9,17 @@ import kotlinx.coroutines.*
9
9
import kotlinx.coroutines.channels.*
10
10
import kotlin.system.*
11
11
12
- suspend fun CoroutineScope. massiveRun (action : suspend () -> Unit ) {
12
+ suspend fun massiveRun (action : suspend () -> Unit ) {
13
13
val n = 100 // number of coroutines to launch
14
14
val k = 1000 // times an action is repeated by each coroutine
15
15
val time = measureTimeMillis {
16
- val jobs = List (n) {
17
- launch {
18
- repeat(k) { action() }
16
+ coroutineScope { // scope for coroutines
17
+ repeat(n) {
18
+ launch {
19
+ repeat(k) { action() }
20
+ }
19
21
}
20
22
}
21
- jobs.forEach { it.join() }
22
23
}
23
24
println (" Completed ${n * k} actions in $time ms" )
24
25
}
@@ -39,16 +40,18 @@ fun CoroutineScope.counterActor() = actor<CounterMsg> {
39
40
}
40
41
}
41
42
42
- fun main () = runBlocking<Unit > {
43
43
// sampleStart
44
+ fun main () = runBlocking {
44
45
val counter = counterActor() // create the actor
45
- GlobalScope .massiveRun {
46
- counter.send(IncCounter )
46
+ withContext(Dispatchers .Default ) {
47
+ massiveRun {
48
+ counter.send(IncCounter )
49
+ }
47
50
}
48
51
// send a message to get a counter value from an actor
49
52
val response = CompletableDeferred <Int >()
50
53
counter.send(GetCounter (response))
51
54
println (" Counter = ${response.await()} " )
52
55
counter.close() // shutdown the actor
53
- // sampleEnd
54
56
}
57
+ // sampleEnd
Original file line number Diff line number Diff line change @@ -13,14 +13,6 @@ class SharedStateGuideTest {
13
13
)
14
14
}
15
15
16
- @Test
17
- fun testKotlinxCoroutinesGuideSync01b () {
18
- test(" KotlinxCoroutinesGuideSync01b" ) { kotlinx.coroutines.guide.sync01b.main() }.verifyLinesStart(
19
- " Completed 100000 actions in" ,
20
- " Counter ="
21
- )
22
- }
23
-
24
16
@Test
25
17
fun testKotlinxCoroutinesGuideSync02 () {
26
18
test(" KotlinxCoroutinesGuideSync02" ) { kotlinx.coroutines.guide.sync02.main() }.verifyLinesStart(
You can’t perform that action at this time.
0 commit comments