fun main() { Topics.sub("ABC") Topics.sub("ZXC") while (true) { val take = Topics.topics.take() println(take) } } object Topics { val topics = LinkedBlockingQueue() fun sub(name: String) { sim { topics.put("FROM $name $it") } } // Imagine this is actually a method from a Java lib. Takes callback and does its thing. private fun sim(cb: (String) -> Unit): () -> Unit { Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate({ cb(Random.nextInt().toString()) }, 0, 500, TimeUnit.MILLISECONDS) return { println("Cancel token invoked.")} } }