Kotlin协程通道发送内部fixedRateTimer

Kotlin协程通道发送内部fixedRateTimer,kotlin,kotlin-coroutines,kotlin-coroutines-flow,kotlin-coroutine-channel,Kotlin,Kotlin Coroutines,Kotlin Coroutines Flow,Kotlin Coroutine Channel,我正在做一个爱好项目,第一次使用Kotlin合作项目。我已经阅读并观看了关于它的视频,我有点明白了这个概念。但我遇到了一个问题。让我给你看看我的代码 package com.dev.tuber.ingestion.snapshots import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.coroutineScope import org.joda.time.LocalTime import java.util.

我正在做一个爱好项目,第一次使用Kotlin合作项目。我已经阅读并观看了关于它的视频,我有点明白了这个概念。但我遇到了一个问题。让我给你看看我的代码

package com.dev.tuber.ingestion.snapshots

import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.coroutineScope
import org.joda.time.LocalTime
import java.util.*
import java.util.concurrent.ConcurrentHashMap
import kotlin.concurrent.fixedRateTimer

object SnapshotsBuffer {
    private val buffer = ConcurrentHashMap<Int, MutableMap<Int, Queue<Snapshot>>>()

    init {
        for (minute in 0..59) {
            buffer[minute] = mutableMapOf()
        }
    }

    suspend fun start(snapshotsChannel: Channel<Snapshot>, composeSnapshots: Channel<MutableMap<Int, Queue<Snapshot>>>) {
        startComposing(composeSnapshots)

        for (snapshot in snapshotsChannel) {
            val currentMinute = getCurrentMinute()
            if (!buffer[currentMinute]!!.containsKey(snapshot.pair.id)) {
                buffer[currentMinute]!![snapshot.pair.id] = LinkedList()
            }

            buffer[currentMinute]!![snapshot.pair.id]!!.add(snapshot)
            println(buffer)
        }
    }

    private fun startComposing(composeSnapshots: Channel<MutableMap<Int, Queue<Snapshot>>>) {
        val oneMinute = (1000 * 60).toLong()

        fixedRateTimer("consuming", true, oneMinute, oneMinute) {
            val previousMinute = getPreviousMinute()
            composeSnapshots.send(buffer[previousMinute]!!) <---- cannot do this
            buffer[getPreviousMinute()] = mutableMapOf()
        }
    }

    private fun getCurrentMinute(): Int {
        return LocalTime().minuteOfHour
    }

    private fun getPreviousMinute(): Int {
        val currentMinute = getCurrentMinute()

        if(currentMinute == 0) return 59
        return currentMinute - 1
    }
}
package com.dev.tuber.inspection.snapshots
导入kotlinx.coroutines.channels.Channel
导入kotlinx.coroutines.coroutineScope
导入org.joda.time.LocalTime
导入java.util*
导入java.util.concurrent.ConcurrentHashMap
导入kotlin.concurrent.fixedRateTimer
对象快照缓冲区{
private val buffer=ConcurrentHashMap()
初始化{
用于(0..59分钟){
buffer[minute]=mutableMapOf()
}
}
暂停有趣的开始(快照频道:频道,合成快照:频道){
开始合成(合成快照)
用于(snapshotsChannel中的快照){
val currentMinute=getCurrentMinute()
如果(!buffer[currentMinute]!!.containsKey(snapshot.pair.id)){
缓冲区[currentMinute]!![snapshot.pair.id]=LinkedList()
}
缓冲区[currentMinute]!![snapshot.pair.id]!!.add(快照)
println(缓冲区)
}
}
私人娱乐开始合成(合成快照:频道){
val一分钟=(1000*60).toLong()
fixedRateTimer(“消费”,真,一分钟,一分钟){
val previousMinute=getPreviousMinute()

composeSnapshots.send(缓冲区[previousMinute]!!)您不能从非挂起函数调用挂起函数(
挂起娱乐频道.send(元素:E)

以协同路由的方式,您可以有一个无限循环,它会暂停一分钟,并反复发送到通道。最重要的是,延迟是对取消的合作

private-suspend-fun-start合成(合成快照:频道){
val一分钟=(1000*60).toLong()
while(true){
延迟(一分钟)
val previousMinute=getPreviousMinute()
composeSnapshots.send(缓冲区[上一分钟]!!)
缓冲区[previousMinute]=mutableMapOf()
}
}