关于 Future 类 boolean cancel(boolean mayInterruptIfRunning) 的疑问
程序员文章站
2022-05-28 08:46:46
...
boolean cancel(boolean mayInterruptIfRunning) 方法描述:
/**
* Attempts to cancel execution of this task. This attempt will
* fail if the task has already completed, has already been cancelled,
* or could not be cancelled for some other reason. If successful,
* and this task has not started when <tt>cancel</tt> is called,
* this task should never run. If the task has already started,
* then the <tt>mayInterruptIfRunning</tt> parameter determines
* whether the thread executing this task should be interrupted in
* an attempt to stop the task.
*
* <p>After this method returns, subsequent calls to {@link #isDone} will
* always return <tt>true</tt>. Subsequent calls to {@link #isCancelled}
* will always return <tt>true</tt> if this method returned <tt>true</tt>.
*
* @param mayInterruptIfRunning <tt>true</tt> if the thread executing this
* task should be interrupted; otherwise, in-progress tasks are allowed
* to complete
* @return <tt>false</tt> if the task could not be cancelled,
* typically because it has already completed normally;
* <tt>true</tt> otherwise
*/
但是我实际在进行测试的时候发现,不管传入是 true,还是 false,都会被中断. 这是为啥?
测试代码:
ExecutorService executorService = null;
{
executorService = Executors.newFixedThreadPool(2);
}
@Test
public void cancleTest(){
Future<?> submit = executorService.submit(new Run());
boolean cancel = submit.cancel(false);
System.out.println("是否被取消: " + cancel);
}
@Test
public void cancleTest002(){
Future<?> submit = executorService.submit(new Run());
boolean cancel = submit.cancel(true);
System.out.println("是否被取消: " + cancel);
}
class Run implements Runnable{
public void run() {
System.out.println("Runable 被执行了...");
for (long i = 0; i < 10000000000L; i++) {
System.out.println("iii"+i);
}
System.out.println("我是最后一行...");
}
}
结果:
Runable 被执行了...
iii0
iii1
iii2
iii3
iii4
iii5
iii6
iii7
iii8
iii9
iii10
iii11
iii12
iii13
iii14
iii15
iii16
iii17
iii18
iii19
iii20
iii21
iii22
iii23
iii24
iii25
iii26
iii27
iii28
iii29
iii30
iii31
iii32
iii33
iii34
iii35
iii36
iii37
iii38
iii39
iii40
iii41
iii42
iii43
iii44
iii45
iii46
iii47
iii48
iii49
iii50
iii51
iii52
iii53
iii54
iii55
iii56
iii57
iii58
iii59
iii60
iii61
iii62
iii63
iii64
iii65
iii66
iii67
iii68
iii69
iii70
iii71
iii72
是否被取消: true
iii73
iii74
iii75
iii76
iii77
iii78
iii79
iii80
iii81
iii82
iii83
iii84
iii85
iii86
iii87
iii88
iii89
iii90
iii91
iii92
iii93
iii94
iii95
iii96
iii97
iii98
iii99
iii100
iii101
iii102
iii103
iii104
iii105
iii106
iii107
iii108
iii109
iii110
iii111
iii112
iii113
iii114
iii115
iii116
iii117
iii118
iii119
iii120
iii121
iii122
iii123
iii124
iii125
iii126
iii127
iii128
iii129
iii130
iii131
iii132
iii133
iii134
iii135
iii136
iii137
iii138
iii139
iii140
iii141
iii142
iii143
iii144
iii145
iii146
iii147
iii148
iii149
iii150
iii151
iii152
iii153
iii154
iii155
iii156
iii157
iii158
iii159
iii160
iii161
iii162
iii163
iii164
iii165
iii166
iii167
iii168
iii169
iii170
iii171
iii172
iii173
iii174
iii175
iii176
iii177
iii178
iii179
iii180
iii181
iii182
iii183
iii184
iii185
iii186
iii187
iii188
iii189
iii190
iii191
iii192
iii193
iii194
iii195
iii196
iii197
iii198
iii199
iii200
iii201
iii202
iii203
iii204
iii205
iii206
iii207
iii208
iii209
iii210
iii211
iii212
iii213
iii214
iii215
iii216
iii217
iii218
iii219
iii220
iii221
iii222
iii223
iii224
iii225
iii226
iii227
iii228
iii229
iii230
iii231
iii232
iii233
iii234
iii235
iii236
iii237
iii238
iii239
iii240
iii241
iii242
iii243
iii244
iii245
iii246
iii247
iii248
iii249
iii250
iii251
iii252
iii253
iii254
iii255
iii256
iii257
iii258
iii259
iii260
iii261
iii262
iii263
iii264
iii265
iii266
iii267
iii268
iii269
iii270
iii271
iii272
iii273
iii274
iii275
iii276
iii277
iii278
iii279
iii280
iii281
iii282
iii283
iii284
iii285
iii286
iii287
iii288
iii289
iii290
iii291
iii292
iii293
iii294
iii295
iii296
iii297
iii298
iii299
iii300
iii301
iii302
iii303
iii304
iii305
iii306
iii307
iii308
iii309
iii310
iii311
iii312
iii313
iii314
iii315
iii316
iii317
iii318
iii319
望各位大佬们不吝指点下,感谢!
已解决,junit 框架的问题,代码上的注释没有问题.
/**
* Attempts to cancel execution of this task. This attempt will
* fail if the task has already completed, has already been cancelled,
* or could not be cancelled for some other reason. If successful,
* and this task has not started when <tt>cancel</tt> is called,
* this task should never run. If the task has already started,
* then the <tt>mayInterruptIfRunning</tt> parameter determines
* whether the thread executing this task should be interrupted in
* an attempt to stop the task.
*
* <p>After this method returns, subsequent calls to {@link #isDone} will
* always return <tt>true</tt>. Subsequent calls to {@link #isCancelled}
* will always return <tt>true</tt> if this method returned <tt>true</tt>.
*
* @param mayInterruptIfRunning <tt>true</tt> if the thread executing this
* task should be interrupted; otherwise, in-progress tasks are allowed
* to complete
* @return <tt>false</tt> if the task could not be cancelled,
* typically because it has already completed normally;
* <tt>true</tt> otherwise
*/
但是我实际在进行测试的时候发现,不管传入是 true,还是 false,都会被中断. 这是为啥?
测试代码:
ExecutorService executorService = null;
{
executorService = Executors.newFixedThreadPool(2);
}
@Test
public void cancleTest(){
Future<?> submit = executorService.submit(new Run());
boolean cancel = submit.cancel(false);
System.out.println("是否被取消: " + cancel);
}
@Test
public void cancleTest002(){
Future<?> submit = executorService.submit(new Run());
boolean cancel = submit.cancel(true);
System.out.println("是否被取消: " + cancel);
}
class Run implements Runnable{
public void run() {
System.out.println("Runable 被执行了...");
for (long i = 0; i < 10000000000L; i++) {
System.out.println("iii"+i);
}
System.out.println("我是最后一行...");
}
}
结果:
Runable 被执行了...
iii0
iii1
iii2
iii3
iii4
iii5
iii6
iii7
iii8
iii9
iii10
iii11
iii12
iii13
iii14
iii15
iii16
iii17
iii18
iii19
iii20
iii21
iii22
iii23
iii24
iii25
iii26
iii27
iii28
iii29
iii30
iii31
iii32
iii33
iii34
iii35
iii36
iii37
iii38
iii39
iii40
iii41
iii42
iii43
iii44
iii45
iii46
iii47
iii48
iii49
iii50
iii51
iii52
iii53
iii54
iii55
iii56
iii57
iii58
iii59
iii60
iii61
iii62
iii63
iii64
iii65
iii66
iii67
iii68
iii69
iii70
iii71
iii72
是否被取消: true
iii73
iii74
iii75
iii76
iii77
iii78
iii79
iii80
iii81
iii82
iii83
iii84
iii85
iii86
iii87
iii88
iii89
iii90
iii91
iii92
iii93
iii94
iii95
iii96
iii97
iii98
iii99
iii100
iii101
iii102
iii103
iii104
iii105
iii106
iii107
iii108
iii109
iii110
iii111
iii112
iii113
iii114
iii115
iii116
iii117
iii118
iii119
iii120
iii121
iii122
iii123
iii124
iii125
iii126
iii127
iii128
iii129
iii130
iii131
iii132
iii133
iii134
iii135
iii136
iii137
iii138
iii139
iii140
iii141
iii142
iii143
iii144
iii145
iii146
iii147
iii148
iii149
iii150
iii151
iii152
iii153
iii154
iii155
iii156
iii157
iii158
iii159
iii160
iii161
iii162
iii163
iii164
iii165
iii166
iii167
iii168
iii169
iii170
iii171
iii172
iii173
iii174
iii175
iii176
iii177
iii178
iii179
iii180
iii181
iii182
iii183
iii184
iii185
iii186
iii187
iii188
iii189
iii190
iii191
iii192
iii193
iii194
iii195
iii196
iii197
iii198
iii199
iii200
iii201
iii202
iii203
iii204
iii205
iii206
iii207
iii208
iii209
iii210
iii211
iii212
iii213
iii214
iii215
iii216
iii217
iii218
iii219
iii220
iii221
iii222
iii223
iii224
iii225
iii226
iii227
iii228
iii229
iii230
iii231
iii232
iii233
iii234
iii235
iii236
iii237
iii238
iii239
iii240
iii241
iii242
iii243
iii244
iii245
iii246
iii247
iii248
iii249
iii250
iii251
iii252
iii253
iii254
iii255
iii256
iii257
iii258
iii259
iii260
iii261
iii262
iii263
iii264
iii265
iii266
iii267
iii268
iii269
iii270
iii271
iii272
iii273
iii274
iii275
iii276
iii277
iii278
iii279
iii280
iii281
iii282
iii283
iii284
iii285
iii286
iii287
iii288
iii289
iii290
iii291
iii292
iii293
iii294
iii295
iii296
iii297
iii298
iii299
iii300
iii301
iii302
iii303
iii304
iii305
iii306
iii307
iii308
iii309
iii310
iii311
iii312
iii313
iii314
iii315
iii316
iii317
iii318
iii319
望各位大佬们不吝指点下,感谢!
已解决,junit 框架的问题,代码上的注释没有问题.