Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Kotlin 主函数在协同路由之前终止_Kotlin_Kotlinx.coroutines - Fatal编程技术网

Kotlin 主函数在协同路由之前终止

Kotlin 主函数在协同路由之前终止,kotlin,kotlinx.coroutines,Kotlin,Kotlinx.coroutines,我正在做一个面向过程编程课程的项目 我试图在一个协同程序中启动两个对象(生产者和消费者),然后让生产者被消费。然后我告诉主函数等待协同程序完成Job.join()。然而,这似乎不起作用。因为invokeOnCompletion函数是在生产/消费周期开始之前执行的。我已经包括了下面的代码和输出,如果您有任何意见,我将不胜感激 这是面向过程编程课程中的一个项目。最终目标是生成多个对象实例,并让它们使用通道进行通信。我以前从未用kotlin编写过代码,也从未使用过协同程序。这是可能的,还是我误解了协同

我正在做一个面向过程编程课程的项目

我试图在一个协同程序中启动两个对象(生产者和消费者),然后让生产者被消费。然后我告诉主函数等待协同程序完成Job.join()。然而,这似乎不起作用。因为invokeOnCompletion函数是在生产/消费周期开始之前执行的。我已经包括了下面的代码和输出,如果您有任何意见,我将不胜感激

这是面向过程编程课程中的一个项目。最终目标是生成多个对象实例,并让它们使用通道进行通信。我以前从未用kotlin编写过代码,也从未使用过协同程序。这是可能的,还是我误解了协同程序

class Producer(val idvar: Int) {
    var resource: Int = 10

    init {
        println("${idvar} was created.")
    }

    suspend fun getConsumed(channel: Channel<Int>) = produce<Int> {
        println("Someone is trying to consume me...")
        while(true) {
            channel.send(1)
            resource--
            println("I have ${resource} things left.")
            if(resource <= 0) {
                break
            }
            var sleep = ((Random().nextDouble())*1000).toLong()
            println("sleeping for ${sleep} ms")
            delay(1000)

        }
        println("I'm used up! I have ${resource} resources.")

    }
}

class Consumer() {
    var resource: Int = 0

    suspend fun consume(producer: Producer?, channel: Channel<Int>) = produce<Int> {
        println("Trying to consume producer: ${producer?.resource}")
        producer?.getConsumed(channel)
        while(true) {
            var recv = channel.receive()
            if(recv == 0) break
            resource += recv
        }
        println("Consumed in total: " + resource)
    }
}


fun main(args: Array<String>) = runBlocking<Unit> {

    val prodJob = launch {
        var prodObj: Producer = Producer(1)
        var consumObj: Consumer = Consumer()
        val chan = Channel<Int>()
        consumObj.consume(prodObj, chan)

        // Why does this println print before the above line is complete?
        println("Comsumption done.")
    }

    // invokeOnCompletion is executed before production/consumption is done?
    prodJob.invokeOnCompletion { println("Consumption is done?") }

    // Main could do other stuff here. 

    // Since prodJob is regarded as complete, main joins instantly.
    prodJob.join()
    println("Joined...")
}
Consumer.consume()
-方法不正确。移除
=product
零件,它将工作:

suspend fun consume(producer: Producer?, channel: Channel<Int>) {
    println("Trying to consume producer: ${producer?.resource}")
    producer?.getConsumed(channel)
    while(true) {
        var recv = channel.receive()
        if(recv == 0) break
        resource += recv
    }
    println("Consumed in total: " + resource)
}
suspend fun consume(producer: Producer?, channel: Channel<Int>) {
    println("Trying to consume producer: ${producer?.resource}")
    producer?.getConsumed(channel)
    while(true) {
        var recv = channel.receive()
        if(recv == 0) break
        resource += recv
    }
    println("Consumed in total: " + resource)
}
suspend fun getConsumed(channel: Channel<Int>) = launch {
    println("Someone is trying to consume me...")
    while(true) {
        channel.send(1)
        resource--
        println("I have ${resource} things left.")
        if(resource <= 0) {
            break
        }
        var sleep = ((Random().nextDouble())*1000).toLong()
        println("sleeping for ${sleep} ms")
        delay(1000)

    }
    println("I'm used up! I have ${resource} resources.")

}