Thead.join matando hilo stream java (kotlin)

Tengo un object administrado (Dropwizard) que está procesando una queue en la memory. Cuando se envía la señal de apagado, necesito procesar el final de la queue. Cuando establezco la señal de apagado en verdadero sin unirme al hilo, puedo ver si pongo un sueño en mi testing de que los artículos son procesados. Sin embargo, cuando me uno al hilo parece detener el hilo en lugar de esperar a que termine el método de ejecución.

class LedgerProcessor constructor( private val ledgerProcessorQueue: LedgerProcessorQueue, private val paymentDao: PaymentDao) : Managed { private lateinit var pollingThread: Thread private val shuttingDown: AtomicBoolean = AtomicBoolean(false) override fun start() { log.info { "Started Processing ledger requests" } pollingThread = object : Thread() { override fun run() { while (checkStatus()) { val payment = ledgerProcessorQueue.next() log.info { "Upserting payment ${payment.tokenId} status: ${payment.status}" } paymentDao.upsert(payment) } } } pollingThread.start() } private fun checkStatus(): Boolean { val isShuttingDown = shuttingDown.get() val hasMore = ledgerProcessorQueue.hasNext() log.info { "isShuttingDown: $isShuttingDown, hasMore: $hasMore - ${!isShuttingDown || hasMore}" } return !isShuttingDown || hasMore } override fun stop() { shuttingDown.set(true) pollingThread.join() // this seems to stop the run in process } 

Esta testing falla cuando pollingThread.join () está presente pero pasa cuando no hay unión

  @Test fun whenStartedButStoppedBeforeAllProcessedThenAllPaymentsInQueueShouldBeProcessedBeforeEnd(){ val payment0 = aPayment() val payment1 = aPayment() val payment2 = aPayment() ledgerQueue.add(payment0) ledgerQueue.add(payment1) ledgerQueue.add(payment2) whenever(paymentDao.upsert(payment0)).then { ledgerProcessor.stop() } ledgerProcessor.start() //Thread.sleep(5000) verify(paymentDao).upsert(payment0) verify(paymentDao).upsert(payment1) verify(paymentDao).upsert(payment2) } 

Test Log con Thread.join en:

 INFO [12:02:44.734] [main] cwpdlLedgerProcessor - Started Processing ledger requests INFO [12:02:44.738] [Thread-0] cwpdlLedgerProcessor - isShuttingDown: false, hasMore: true - true INFO [12:02:44.739] [Thread-0] cwpdlLedgerProcessor - Upserting payment 61a20508-37e6-4347-a7d1-2538fda8c470 status: Processing 

logging de testing con Thread.join off:

 INFO [12:04:41.994] [main] cwpdlLedgerProcessor - Started Processing ledger requests INFO [12:04:41.999] [Thread-0] cwpdlLedgerProcessor - isShuttingDown: false, hasMore: true - true INFO [12:04:41.999] [Thread-0] cwpdlLedgerProcessor - Upserting payment 5de161b7-3158-4ee4-a5d7-a690b3b500d6 status: Processing INFO [12:04:42.004] [Thread-0] cwpdlLedgerProcessor - isShuttingDown: true, hasMore: true - true INFO [12:04:42.004] [Thread-0] cwpdlLedgerProcessor - Upserting payment 741ab5ee-48af-426c-b167-97641286e796 status: Processing INFO [12:04:42.004] [Thread-0] cwpdlLedgerProcessor - isShuttingDown: true, hasMore: true - true INFO [12:04:42.004] [Thread-0] cwpdlLedgerProcessor - Upserting payment 79546c86-5d02-4bd7-91b8-c2e0c5c4eb5f status: Processing INFO [12:04:42.004] [Thread-0] cwpdlLedgerProcessor - isShuttingDown: true, hasMore: false - false