LINUX.ORG.RU

kotlin акторы. Помогите собрать пример

 , ,


0

1
package com.example.socialkotlin
import android.graphics.Color.*
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import java.util.concurrent.Executors
import kotlin.system.*
import kotlinx.android.synthetic.main.content_main.*
// import kotlinx.coroutines.*
import kotlinx.coroutines.delay
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
// import kotlinx.coroutines.CommonPool
import kotlinx.coroutines.async
import kotlinx.coroutines.AbstractCoroutine
import kotlinx.coroutines.asCoroutineDispatcher
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.CoroutineScope

import java.util.concurrent.*
import kotlinx.coroutines.channels.*
import kotlinx.coroutines.channels.actor



val threadPoolExecutor = Executors.newCachedThreadPool()
// val threadPoolExecutor = Executors.newFixedThreadPool(5)
val threadPool = threadPoolExecutor.asCoroutineDispatcher()

sealed class CounterMsg {
    object IncCounter : CounterMsg() // one-way message to increment counter
    class GetCounter(val response: SendChannel<Int>) : CounterMsg() // a request with channel for reply.
}

fun counterActor() = GlobalScope.actor<CounterMsg>(threadPool) { //(1)
    var counter = 0 //(9) actor state, not shared
    for (msg in channel) { // handle incoming messages
        when (msg) {
            is CounterMsg.IncCounter -> counter++ //(4)
            is CounterMsg.GetCounter -> msg.response.send(counter) //(3)
        }
    }
}

suspend fun getCurrentCount(counter: ActorJob<CounterMsg>): Int { //(8)
    val response = Channel<Int>() //(2)
    counter.send(CounterMsg.GetCounter(response))
    val receive = response.receive()
    println("Counter = $receive")
    return receive
}


class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        Log.d("TAG", "----------------------------")
        
        // val counter = counterActor()
        // GlobalScope.launch(threadPool) {
        //     //(5)
        //     while(getCurrentCount(counter) < 100){
        //         delay(100)
        //         Log.d("TAG", "sending IncCounter message")
        //         counter.send(CounterMsg.IncCounter) //(7)
        //     }
        // }

        // GlobalScope.launch(threadPool) {
        //     //(6)
        //     while (getCurrentCount(counter) < 100) {
        //         delay(200)
        //     }
        // }.join()
        // counter.close() // shutdown the actor

        // GlobalScope.launch {
        //     val channel = basicActor()

        //     channel.send(Message.Increment(1))
        //     channel.send(Message.Increment(2))

        //     val deferred = CompletableDeferred<Int>()

        //     channel.send(Message.GetValue(deferred))

        //     Log.d("TAG", deferred.await().toString()) // prints "3"

        //     channel.close()
        // }

        GlobalScope.launch {
        val counter = counterActor()

        launch(threadPool) { //(5)
            while(getCurrentCount(counter) < 100){
                delay(100)
                println("sending IncCounter message")
                counter.send(CounterMsg.IncCounter) //(7)
            }
        }

        launch(threadPool) { //(6)
        while ( getCurrentCount(counter) < 100) {
            delay(200)
        }
        }.join()
        counter.close() // shutdown the actor
        }
        

        super.onCreate(savedInstanceState)
        setContentView(R.layout.content_main)
        text_id.setText("zzz2")
        // text_id.setText(getWarmth(c).toString())
    }
}

out:

> Task :app:compileDebugKotlin FAILED
e: /home/user/media/source_project/socialkotlin/app/src/main/java/com/example/socialkotlin/MainActivity.kt: (45, 38): Unresolved reference: ActorJob
★★★★

Не найден идентификатор ActorJob. Вероятно тебе нужно добавить нужный import.

Legioner ★★★★★
()
Вы не можете добавлять комментарии в эту тему. Тема перемещена в архив.