Tag: 锁定

在Java / Kotlin的ThreadPool中取消被取代的任务

我试图找出如何取消一个任务在一个ThreadPool如果一个新的任务来取代它。 我有一些类似这样的: class RenderableThing { private val lock = ReentrantLock() fun lock(interrupt: Boolean) { if (lock.isLocked && interrupt) { // How do I interrupt the thread that has the lock? } lock.lock() } fun unlock() = lock.unlock() fun cache() = Cached() inner class Cached() } class BackgroundStuff(threads: Int) { val pool = Executors.newFixedThreadPool(threads)!! fun doBackgroundStuff(thing: […]