Skip to content

Commit 0a1f879

Browse files
committed
Added chapters 14 and 15
1 parent 9335ada commit 0a1f879

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+
}
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+
}

Chapter14/exception_handler.kt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import kotlinx.coroutines.CoroutineExceptionHandler
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+
val handler = CoroutineExceptionHandler { context, throwable ->
9+
println("Intercepted exception of type ${throwable.javaClass.simpleName}")
10+
}
11+
runBlocking {
12+
val job = GlobalScope.launch(handler) {
13+
launch {
14+
println("Starting first child")
15+
delay(1000)
16+
println("First child completed")
17+
}
18+
launch {
19+
println("Second child throwing exception")
20+
delay(100)
21+
throw IndexOutOfBoundsException()
22+
}
23+
}
24+
job.join()
25+
}
26+
}

Chapter14/exception_handler_await.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import kotlinx.coroutines.CoroutineExceptionHandler
2+
import kotlinx.coroutines.GlobalScope
3+
import kotlinx.coroutines.async
4+
import kotlinx.coroutines.runBlocking
5+
6+
fun main() {
7+
val handler = CoroutineExceptionHandler { context, throwable ->
8+
println("Intercepted exception of type ${throwable.javaClass.simpleName}")
9+
}
10+
runBlocking {
11+
val deferred = GlobalScope.async(handler) {
12+
throw IndexOutOfBoundsException()
13+
}
14+
deferred.await()
15+
}
16+
}

Chapter14/first_coroutine.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import kotlinx.coroutines.GlobalScope
2+
import kotlinx.coroutines.delay
3+
import kotlinx.coroutines.launch
4+
5+
fun main() {
6+
GlobalScope.launch {
7+
delay(1000)
8+
println("I am a coroutine")
9+
}
10+
println("I am the main thread")
11+
Thread.sleep(2000)
12+
}

Chapter14/intro.kt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import kotlinx.coroutines.Dispatchers
2+
import kotlinx.coroutines.GlobalScope
3+
import kotlinx.coroutines.delay
4+
import kotlinx.coroutines.launch
5+
6+
fun main() {
7+
GlobalScope.launch {
8+
// create a new global level coroutine
9+
delay(1000L) // wait for 1 second
10+
println("World!")
11+
}
12+
println("Hello,") // main thread continues while coroutine is delayed
13+
Thread.sleep(2000L) // block main thread to keep JVM alive
14+
GlobalScope.launch(context = Dispatchers.IO) {
15+
// will get dispatched to DefaultDispatcher
16+
println("Default : I'm working in thread ${Thread.currentThread().name}")
17+
}
18+
19+
20+
}

Chapter14/jobs.kt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
var x = 0
8+
val job = GlobalScope.launch {
9+
while (true) {
10+
x += 1
11+
delay(10)
12+
println(x)
13+
}
14+
}
15+
runBlocking {
16+
delay(100)
17+
job.cancel()
18+
}
19+
}

Chapter14/join.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
val job = GlobalScope.launch {
8+
delay(1000L)
9+
println("Hello")
10+
}
11+
runBlocking {
12+
job.join()
13+
println("World")
14+
}
15+
}

Chapter14/lazy.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import kotlinx.coroutines.CoroutineStart
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+
val job = GlobalScope.launch(start = CoroutineStart.LAZY) {
9+
delay(100)
10+
println("World")
11+
}
12+
runBlocking {
13+
delay(1000)
14+
println("Hello")
15+
job.join()
16+
}
17+
}

0 commit comments

Comments
 (0)