Skip to content

Commit

Permalink
fix: Wait for the thread to terminate before exiting ProgressReport.s…
Browse files Browse the repository at this point in the history
…top/cancel to ensure that the message 'xxx source files have been analyzed' doesn't appear after the 'Sensor Z PL/SQL Analyzer [plsqlopen] (done)' in SonarScanner.
  • Loading branch information
felipebz committed May 27, 2024
1 parent 776c766 commit d6ab929
Showing 1 changed file with 15 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package org.sonar.plsqlopen.squid

import org.sonar.plsqlopen.utils.log.Logger
import org.sonar.plsqlopen.utils.log.Loggers
import java.util.concurrent.atomic.AtomicBoolean
import java.util.concurrent.locks.Condition
import java.util.concurrent.locks.ReentrantLock
import kotlin.concurrent.withLock
Expand All @@ -37,22 +38,20 @@ class ProgressReport @JvmOverloads constructor(threadName: String,
private var currentFileNumber = -1
private var currentFile: String? = null
private lateinit var iterator: Iterator<String>
private val thread: Thread = Thread(this)
private val thread: Thread = Thread(this, threadName).apply { isDaemon = true }
private var success = false

init {
thread.name = threadName
thread.isDaemon = true
}
private val interrupted = AtomicBoolean().apply { set(false) }

override fun run() {
while (!Thread.interrupted()) {
while (!(interrupted.get() || Thread.currentThread().isInterrupted)) {
try {
Thread.sleep(period)
lock.withLock {
log("$currentFileNumber/$count files analyzed, current file: $currentFile")
}
} catch (e: InterruptedException) {
interrupted.set(true)
thread.interrupt()
break
}

Expand Down Expand Up @@ -86,18 +85,25 @@ class ProgressReport @JvmOverloads constructor(threadName: String,

@Synchronized
fun stop() {
interrupted.set(true)
success = true
thread.interrupt()
join()
}

@Synchronized
fun cancel() {
interrupted.set(true)
thread.interrupt()
join()
}

@Throws(InterruptedException::class)
fun join() {
thread.join()
try {
thread.join()
} catch (e: InterruptedException) {
Thread.currentThread().interrupt();
}
}

private fun log(message: String) {
Expand Down

0 comments on commit d6ab929

Please sign in to comment.