File tree 3 files changed +5
-27
lines changed
kotlinx-coroutines-core/jvm
3 files changed +5
-27
lines changed Original file line number Diff line number Diff line change @@ -117,13 +117,12 @@ private class DispatcherExecutor(@JvmField val dispatcher: CoroutineDispatcher)
117
117
118
118
internal class ExecutorCoroutineDispatcherImpl (override val executor : Executor ) : ExecutorCoroutineDispatcher(), Delay {
119
119
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
- */
125
120
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
+ }
127
126
}
128
127
129
128
override fun dispatch (context : CoroutineContext , block : Runnable ) {
Original file line number Diff line number Diff line change 1
1
package kotlinx.coroutines.internal
2
2
3
- import java.lang.reflect.*
4
3
import java.util.*
5
- import java.util.concurrent.*
6
4
import kotlin.concurrent.withLock as withLockJvm
7
5
8
6
@Suppress(" ACTUAL_WITHOUT_EXPECT" )
@@ -22,20 +20,3 @@ internal actual annotation class BenignDataRace()
22
20
@Suppress(" NOTHING_TO_INLINE" ) // So that R8 can completely remove ConcurrentKt class
23
21
internal actual inline fun <E > identitySet (expectedSize : Int ): MutableSet <E > =
24
22
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
- }
Original file line number Diff line number Diff line change @@ -87,7 +87,6 @@ class RejectedExecutionTest : TestBase() {
87
87
88
88
@Test
89
89
fun testRejectOnDelay () = runTest {
90
- if (! removeFutureOnCancel(executor)) return @runTest // Skip this test on old JDKs
91
90
expect(1 )
92
91
executor.acceptTasks = 1 // accept one task
93
92
assertFailsWith<CancellationException > {
@@ -109,7 +108,6 @@ class RejectedExecutionTest : TestBase() {
109
108
110
109
@Test
111
110
fun testRejectWithTimeout () = runTest {
112
- if (! removeFutureOnCancel(executor)) return @runTest // Skip this test on old JDKs
113
111
expect(1 )
114
112
executor.acceptTasks = 1 // accept one task
115
113
assertFailsWith<CancellationException > {
You can’t perform that action at this time.
0 commit comments