Fix closing all worker threads
Change-Id: Ic7bc6efee46e715b8f44b6f05c6b39e6f68cc931
diff --git a/app/src/main/kotlin/de/ids_mannheim/korapxmltools/KorapXml2Conllu.kt b/app/src/main/kotlin/de/ids_mannheim/korapxmltools/KorapXml2Conllu.kt
index 2bb415e..d8e5e6a 100644
--- a/app/src/main/kotlin/de/ids_mannheim/korapxmltools/KorapXml2Conllu.kt
+++ b/app/src/main/kotlin/de/ids_mannheim/korapxmltools/KorapXml2Conllu.kt
@@ -185,7 +185,8 @@
morpho
)
}
- if (annotateWith != "") {
+ if (annotateWith.isNotEmpty()) {
+ LOGGER.info("closing worker pool")
workerPool?.close()
}
}
diff --git a/app/src/main/kotlin/de/ids_mannheim/korapxmltools/WorkerPool.kt b/app/src/main/kotlin/de/ids_mannheim/korapxmltools/WorkerPool.kt
index 25c2e1d..a8d76c1 100644
--- a/app/src/main/kotlin/de/ids_mannheim/korapxmltools/WorkerPool.kt
+++ b/app/src/main/kotlin/de/ids_mannheim/korapxmltools/WorkerPool.kt
@@ -60,6 +60,8 @@
} catch (e: IOException) {
e.printStackTrace()
+ LOGGER.warning("Worker $it failed: ${e.message}")
+ threads.remove(Thread.currentThread())
}
}.start()
@@ -100,8 +102,14 @@
}
fun close() {
- repeat(numWorkers) {
- queue.offer("#eof")
+ var n = threads.size
+ while(n > 0) {
+ if (queue.offer("#eof")) {
+ n--
+ } else {
+ LOGGER.info("Queue is full, waiting for workers to process")
+ sleep(100)
+ }
}
waitForWorkersToFinish()
}
@@ -110,7 +118,9 @@
while (queue.isNotEmpty()) {
sleep(100) // Wait for queue to empty
}
+ LOGGER.info("Queue is empty, waiting for workers to finish")
threads.forEach(Thread::join)
+ LOGGER.info("All workers finished")
}
}