Skip to content

Commit 7294134

Browse files
committed
Remove a workaround for JDK 6
1 parent bc5d976 commit 7294134

File tree

3 files changed

+5
-27
lines changed

3 files changed

+5
-27
lines changed

kotlinx-coroutines-core/jvm/src/Executors.kt

+5-6
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,12 @@ private class DispatcherExecutor(@JvmField val dispatcher: CoroutineDispatcher)
117117

118118
internal class ExecutorCoroutineDispatcherImpl(override val executor: Executor) : ExecutorCoroutineDispatcher(), Delay {
119119

120-
/*
121-
* Attempts to reflectively (to be Java 6 compatible) invoke
122-
* ScheduledThreadPoolExecutor.setRemoveOnCancelPolicy in order to cleanup
123-
* internal scheduler queue on cancellation.
124-
*/
125120
init {
126-
removeFutureOnCancel(executor)
121+
/* Attempt to invoke ScheduledThreadPoolExecutor.setRemoveOnCancelPolicy in order to clean up
122+
* the internal scheduler queue on cancellation. */
123+
if (executor is ScheduledThreadPoolExecutor) {
124+
executor.removeOnCancelPolicy = true
125+
}
127126
}
128127

129128
override fun dispatch(context: CoroutineContext, block: Runnable) {
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package kotlinx.coroutines.internal
22

3-
import java.lang.reflect.*
43
import java.util.*
5-
import java.util.concurrent.*
64
import kotlin.concurrent.withLock as withLockJvm
75

86
@Suppress("ACTUAL_WITHOUT_EXPECT")
@@ -22,20 +20,3 @@ internal actual annotation class BenignDataRace()
2220
@Suppress("NOTHING_TO_INLINE") // So that R8 can completely remove ConcurrentKt class
2321
internal actual inline fun <E> identitySet(expectedSize: Int): MutableSet<E> =
2422
Collections.newSetFromMap(IdentityHashMap(expectedSize))
25-
26-
private val REMOVE_FUTURE_ON_CANCEL: Method? = try {
27-
ScheduledThreadPoolExecutor::class.java.getMethod("setRemoveOnCancelPolicy", Boolean::class.java)
28-
} catch (e: Throwable) {
29-
null
30-
}
31-
32-
@Suppress("NAME_SHADOWING")
33-
internal fun removeFutureOnCancel(executor: Executor): Boolean {
34-
try {
35-
val executor = executor as? ScheduledThreadPoolExecutor ?: return false
36-
(REMOVE_FUTURE_ON_CANCEL ?: return false).invoke(executor, true)
37-
return true
38-
} catch (e: Throwable) {
39-
return false // failed to setRemoveOnCancelPolicy, assume it does not removes future on cancel
40-
}
41-
}

kotlinx-coroutines-core/jvm/test/RejectedExecutionTest.kt

-2
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ class RejectedExecutionTest : TestBase() {
8787

8888
@Test
8989
fun testRejectOnDelay() = runTest {
90-
if (!removeFutureOnCancel(executor)) return@runTest // Skip this test on old JDKs
9190
expect(1)
9291
executor.acceptTasks = 1 // accept one task
9392
assertFailsWith<CancellationException> {
@@ -109,7 +108,6 @@ class RejectedExecutionTest : TestBase() {
109108

110109
@Test
111110
fun testRejectWithTimeout() = runTest {
112-
if (!removeFutureOnCancel(executor)) return@runTest // Skip this test on old JDKs
113111
expect(1)
114112
executor.acceptTasks = 1 // accept one task
115113
assertFailsWith<CancellationException> {

0 commit comments

Comments
 (0)