Hi I am writing some code for using coroutine to chain a channel to a background prcess in an intellij idea plugin like the following snippet. All lines with the `~>` comment start a coroutine. Say the method update will be called in multiple thread. I am wondering if the synchronized lock work or not. It seems wrong but I don’t know if there is better way for this. Any idea? Thanks in advanced!
private val processingInfoChannel = Channel<FileProgressProcessingInfo>()
private var processing : Boolean = false
@Synchronized
fun update(info: FileProgressProcessingInfo) {
if (!processing) {
if (info.processing.isEmpty()) {
return
}
scope.launch {
/*~>*/ withBackgroundProgress(project, LspConstants.FILE_PROGRESS, false) {
/*~>*/ withProgressText(file) {
/*~>*/ reportProgress {reporter ->
var workSize = 0
/*~>*/ reporter.sizedStep(workSize) { }
while (workSize != 100) {
/*~>*/ val info = processingInfoChannel.receive()
workSize = if (info.processing.isEmpty()) {
100
} else {
info.processing[0].range.start.line*100/info.processing[0].range.end.line
}
/*~>*/ reporter.sizedStep(workSize) { }
}
}
}
processing = false
}
}
processing = true
}
scope.launch {
/*~>*/ processingInfoChannel.send(info)
}
}
I ran and checked the above code. I am expecting if it’s some fault in it and looking for better solution