Browse Source

[final drafts] Updated code according to the final drafts of chapters 7 and 8

master
Miguel Castiblanco 5 years ago
parent
commit
10fed60a08
  1. 1
      build.gradle
  2. 3
      src/main/kotlin/chapter7/actor/actor.kt
  3. 10
      src/main/kotlin/chapter7/actor/actorCounter.kt
  4. 2
      src/main/kotlin/chapter7/actor/interaction/actor_samples.kt
  5. 2
      src/main/kotlin/chapter7/confinement/thread_confinement.kt
  6. 1
      src/main/kotlin/chapter7/volatile/volatile.kt
  7. 14
      src/main/kotlin/chapter8/debugging.kt
  8. 10
      src/main/kotlin/chapter8/sample_app.kt
  9. 8
      src/test/kotlin/chapter8/SampleAppFT.kt

1
build.gradle

@ -24,7 +24,6 @@ dependencies {
compile "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version"
// Test libraries
compile "org.jetbrains.kotlin:kotlin-reflect"
testCompile "org.jetbrains.kotlin:kotlin-test"
testCompile "org.jetbrains.kotlin:kotlin-test-junit"

3
src/main/kotlin/chapter7/actor/actor.kt

@ -1,5 +1,6 @@
package chapter7.actor
import kotlinx.coroutines.experimental.CommonPool
import kotlinx.coroutines.experimental.async
import kotlinx.coroutines.experimental.runBlocking
@ -22,7 +23,7 @@ fun asyncIncrement(by: Int) = async {
}
}
fun asyncDecrement(by: Int) = async {
fun asyncDecrement(by: Int) = async(CommonPool) {
for (i in 0 until by) {
actorCounter.send(Action.DECREASE)
}

10
src/main/kotlin/chapter7/actor/actorCounter.kt

@ -4,19 +4,17 @@ import kotlinx.coroutines.experimental.channels.actor
import kotlinx.coroutines.experimental.newSingleThreadContext
private var counter = 0
private var context = newSingleThreadContext("actor")
private val context = newSingleThreadContext("actor")
enum class Action {
INCREASE,
DECREASE
}
fun getCounter() : Int {
return counter
}
fun getCounter() = counter
var actorCounter = actor<Action>(context) {
for (msg in channel){
val actorCounter = actor<Action>(context) {
for (msg in channel) {
when(msg) {
Action.INCREASE -> counter++
Action.DECREASE -> counter--

2
src/main/kotlin/chapter7/actor/interaction/actor_samples.kt

@ -20,6 +20,8 @@ suspend fun bufferedActor() {
bufferedPrinter.send("hello")
bufferedPrinter.send("world")
bufferedPrinter.close()
}
suspend fun actorWithContext() {

2
src/main/kotlin/chapter7/confinement/thread_confinement.kt

@ -17,7 +17,7 @@ fun main(args: Array<String>) = runBlocking {
}
var counter = 0
var context = newSingleThreadContext("counter")
val context = newSingleThreadContext("counter")
fun asyncIncrement(by: Int) = async(context) {
for (i in 0 until by) {

1
src/main/kotlin/chapter7/volatile/volatile.kt

@ -17,7 +17,6 @@ class DataProcessor {
}
}
fun main(args: Array<String>) {
}

14
src/main/kotlin/chapter8/debugging.kt

@ -19,6 +19,10 @@ fun main(args: Array<String>) = runBlocking {
}
private fun threadName() : String {
return Thread.currentThread().name
}
suspend fun automaticNaming() {
val pool = newFixedThreadPoolContext(3, "myPool")
val ctx = newSingleThreadContext("ctx")
@ -26,13 +30,13 @@ suspend fun automaticNaming() {
val tasks = mutableListOf<Deferred<Unit>>()
for (i in 0..5) {
val task = async(pool) {
println("Processing $i in ${Thread.currentThread().name}")
println("Processing $i in ${threadName()}")
withContext(ctx) {
println("Step two of $i happening in thread ${Thread.currentThread().name}")
println("Step two of $i happening in thread ${threadName()}")
}
println("Finishing $i in ${Thread.currentThread().name}")
println("Finishing $i in ${threadName()}")
}
@ -49,10 +53,10 @@ suspend fun specificNaming(){
val ctx = newSingleThreadContext("ctx")
withContext(pool + CoroutineName("main")) {
println("Running in ${Thread.currentThread().name}")
println("Running in ${threadName()}")
withContext(ctx + CoroutineName("inner")) {
println("Switching to ${Thread.currentThread().name}")
println("Switching to ${threadName()}")
}
}
}

10
src/main/kotlin/chapter8/sample_app.kt

@ -11,11 +11,11 @@ data class User(
val profession: String
)
class UserManager(private val datasource: Datasource) {
class UserManager(private val dataSource: DataSource) {
suspend fun getUser(id: Int): User {
val name = datasource.getNameAsync(id)
val age = datasource.getAgeAsync(id)
val profession = datasource.getProfessionAsync(id)
val name = dataSource.getNameAsync(id)
val age = dataSource.getAgeAsync(id)
val profession = dataSource.getProfessionAsync(id)
// Wait for each of them, don't assume they are ready
return User(name.await(),
@ -24,7 +24,7 @@ class UserManager(private val datasource: Datasource) {
}
}
interface Datasource {
interface DataSource {
fun getNameAsync(id: Int): Deferred<String>
fun getAgeAsync(id: Int): Deferred<Int>
fun getProfessionAsync(id: Int): Deferred<String>

8
src/test/kotlin/chapter8/SampleAppFT.kt

@ -11,7 +11,7 @@ class SampleAppFT {
@Test
fun testHappyPath() = runBlocking {
val manager = UserManager(MockDatasource())
val manager = UserManager(MockDataSource())
val user = manager.getUser(10)
assertTrue { user.name == "Susan Calvin" }
@ -21,7 +21,7 @@ class SampleAppFT {
@Test
fun testOppositeOrder() = runBlocking {
val manager = UserManager(MockOppositeDatasource())
val manager = UserManager(MockSlowDbDataSource())
val user = manager.getUser(10)
assertTrue { user.name == "Susan Calvin" }
@ -31,7 +31,7 @@ class SampleAppFT {
}
// Mock a datasource that retrieves the data in a different order
class MockOppositeDatasource: Datasource {
class MockSlowDbDataSource: DataSource {
// Mock getting the name from the database
override fun getNameAsync(id: Int) = async {
delay(1000)
@ -52,7 +52,7 @@ class MockOppositeDatasource: Datasource {
}
// Mock a datasource that retrieves the data in the expected order
class MockDatasource: Datasource {
class MockDataSource: DataSource {
// Mock getting the name from the database
override fun getNameAsync(id: Int) = async {
delay(200)

Loading…
Cancel
Save