Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 01abbab

Browse files
Merge pull request PacktPublishing#4 from sksamuel/master
Added chapters 14 and 15
2 parents 9335ada + 0a1f879 commit 01abbab

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+882
-0
lines changed

‎Chapter14/async.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import kotlinx.coroutines.async
2+
import kotlinx.coroutines.delay
3+
import kotlinx.coroutines.runBlocking
4+
5+
fun main() {
6+
runBlocking {
7+
val deferred = async {
8+
println("Computing value")
9+
delay(1000)
10+
"result"
11+
}
12+
val result = deferred.await()
13+
println("The result is $result")
14+
}
15+
}

‎Chapter14/cancel_children.kt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import kotlinx.coroutines.delay
2+
import kotlinx.coroutines.launch
3+
import kotlinx.coroutines.runBlocking
4+
5+
fun main() {
6+
suspend fun child1() {
7+
println("Starting child 1")
8+
delay(2000)
9+
println("Completing child 1")
10+
}
11+
suspend fun child2() {
12+
println("Starting child 2")
13+
delay(1000)
14+
throw RuntimeException("Boom")
15+
}
16+
try {
17+
runBlocking {
18+
launch {
19+
child1()
20+
}
21+
launch {
22+
child2()
23+
}
24+
}
25+
} catch (e: Exception) {
26+
println("Failed with ${e.message}")
27+
}
28+
}

‎Chapter14/child_failure.kt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import kotlinx.coroutines.delay
2+
import kotlinx.coroutines.launch
3+
import kotlinx.coroutines.runBlocking
4+
5+
fun main() {
6+
suspend fun child1() {
7+
println("Starting child 1")
8+
delay(2000)
9+
println("Completing child 1")
10+
}
11+
suspend fun child2() {
12+
println("Starting child 2")
13+
delay(1000)
14+
throw RuntimeException("Boom")
15+
}
16+
try {
17+
runBlocking {
18+
launch {
19+
child1()
20+
}
21+
launch {
22+
child2()
23+
}
24+
}
25+
} catch (e: Exception) {
26+
println("Failed with ${e.message}")
27+
}
28+
}

‎Chapter14/concurrent_ecommerce.kt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import kotlinx.coroutines.delay
2+
import kotlinx.coroutines.launch
3+
import kotlinx.coroutines.runBlocking
4+
import kotlin.system.measureTimeMillis
5+
6+
fun main() {
7+
8+
suspend fun printShippingLabels() {
9+
delay(3000)
10+
println("After three seconds")
11+
}
12+
13+
suspend fun processPayment() {
14+
delay(2000)
15+
println("Order after two seconds")
16+
}
17+
18+
suspend fun sendEmails() {
19+
delay(1000)
20+
println("Payment after one second")
21+
}
22+
23+
println("Starting")
24+
val time = measureTimeMillis {
25+
runBlocking {
26+
launch {
27+
printShippingLabels()
28+
}
29+
launch {
30+
processPayment()
31+
}
32+
launch {
33+
sendEmails()
34+
}
35+
}
36+
println("runBlocking done")
37+
}
38+
println("Completed in $time ms")
39+
}

‎Chapter14/concurrent_ecommerce_async.kt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import kotlinx.coroutines.async
2+
import kotlinx.coroutines.delay
3+
import kotlinx.coroutines.runBlocking
4+
import kotlin.system.measureTimeMillis
5+
6+
fun main() {
7+
8+
suspend fun fetchOrder(): String {
9+
delay(3000)
10+
return "Order 123"
11+
}
12+
13+
suspend fun fetchAddress(): String {
14+
delay(2000)
15+
return "10 Downing Street"
16+
}
17+
18+
suspend fun fetchUsername(): String {
19+
delay(1000)
20+
return "Prime Minster"
21+
}
22+
23+
println("Starting")
24+
val time = measureTimeMillis {
25+
runBlocking {
26+
val order = async {
27+
fetchOrder()
28+
}
29+
val address = async {
30+
fetchAddress()
31+
}
32+
val username = async {
33+
fetchUsername()
34+
}
35+
println("Shipping ${order.await()} to ${username.await()} at ${address.await()}")
36+
}
37+
}
38+
println("Completed in $time ms")
39+
}

‎Chapter14/debugging.kt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import kotlinx.coroutines.GlobalScope
2+
import kotlinx.coroutines.delay
3+
import kotlinx.coroutines.launch
4+
import kotlinx.coroutines.runBlocking
5+
6+
fun main() {
7+
fun logger(message: String) = println("${Thread.currentThread().name} $message")
8+
val jobs = (1..3).map {
9+
GlobalScope.launch {
10+
repeat(3) {
11+
delay(300)
12+
logger("I'm working hard")
13+
}
14+
}
15+
}
16+
runBlocking {
17+
logger("Starting calculation")
18+
jobs.forEach { it.join() }
19+
}
20+
}

‎Chapter14/debugging_named.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import kotlinx.coroutines.CoroutineName
2+
import kotlinx.coroutines.GlobalScope
3+
import kotlinx.coroutines.delay
4+
import kotlinx.coroutines.launch
5+
import kotlinx.coroutines.runBlocking
6+
7+
fun main() {
8+
fun logger(message: String) = println("${Thread.currentThread().name} $message")
9+
val jobs = listOf("parser", "crawler", "lexer").map {
10+
GlobalScope.launch(CoroutineName(it)) {
11+
repeat(3) {
12+
delay(300)
13+
logger("I'm working hard")
14+
}
15+
}
16+
}
17+
runBlocking {
18+
logger("Starting calculation")
19+
jobs.forEach { it.join() }
20+
}
21+
}

‎Chapter14/dispatcher_default.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import kotlinx.coroutines.Dispatchers
2+
import kotlinx.coroutines.launch
3+
import kotlinx.coroutines.runBlocking
4+
5+
fun main() {
6+
runBlocking {
7+
println("A: " + Thread.currentThread().name)
8+
launch(Dispatchers.Default) {
9+
println("B: " + Thread.currentThread().name)
10+
}
11+
launch {
12+
println("C: " + Thread.currentThread().name)
13+
}
14+
}
15+
}

‎Chapter14/dispatcher_io.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import kotlinx.coroutines.Dispatchers
2+
import kotlinx.coroutines.runBlocking
3+
4+
fun main() {
5+
runBlocking(Dispatchers.IO) {
6+
println(Thread.currentThread().name)
7+
}
8+
}

‎Chapter14/dispatcher_wrapper.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import kotlinx.coroutines.asCoroutineDispatcher
2+
import kotlinx.coroutines.launch
3+
import kotlinx.coroutines.runBlocking
4+
import java.util.concurrent.Executors.newSingleThreadExecutor
5+
6+
fun main() {
7+
runBlocking {
8+
val executor = newSingleThreadExecutor { r -> Thread(r, "my-executor-dispatcher") }
9+
val dispatcher = executor.asCoroutineDispatcher()
10+
launch(dispatcher) {
11+
println(Thread.currentThread().name)
12+
}
13+
launch {
14+
println(Thread.currentThread().name)
15+
}
16+
}
17+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /