`

关于 Future 类 boolean cancel(boolean mayInterruptIfRunning) 的疑问

阅读更多
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 框架的问题,代码上的注释没有问题.
0
0
分享到:
评论

相关推荐

    揭密FutureTask.docx

    boolean cancel(boolean mayInterruptIfRunning); boolean isCancelled(); boolean isDone(); V get() throws InterruptedException, ExecutionException; V get(long timeout, TimeUnit unit) throws ...

    线程超时死掉

    boolean cancel(boolean mayInterruptIfRunning) 取消任务的执行。参数指定是否立即中断任务执行,或者等等任务结束 boolean isCancelled() 任务是否已经取消,任务正常完成前将其取消,则返回true boolean isDone...

    Boolean RT资源包

    Boolean RT资源包,可用于Unity 几何体布尔运算

    Boolean RT.zip

    BooleanRT, 实现2个物体间的布尔运算 The extension provides the following functions: 1- Real-time or per-click Boolean execution. 2- Material, UV and texture preservation. 3- Saving to prefab. 4- ...

    JS字符串false转boolean的方法(推荐)

    下面小编就为大家带来一篇JS字符串false转boolean的方法(推荐)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

    Unity 几何体布尔运算BooleanRT

    Unity 几何体布尔运算BooleanRT,Boolean RT.unitypackage,unity3d布尔运算插件。

    Boolean RT.rar

    BooleanRT实时模型网格布尔运算插件 可以进行实时的模型合并、求差集、求交集等操作。 全网最好用的了,没有第二款。

    Screen Space Boolean Subtract Shader

    Screen Space Boolean Subtract Shader 动态计算模型布尔值,源代码

    boolean_approach

    boolean_approach

    The Complexity of Boolean Functions

    The of Boolean Functions

    Objective-C Boolean 变量

    Objective-C Boolean 变量 具体内容,请参考苹果开发者 Mike 的博文http://blog.sina.com.cn/s/blog_7aa21f320100r6ux.html

    Boolean.Functions.and.Computation.Models,.Clote,.Kranakis,.Springer,.2002

    This textbook presents a survey of research on boolean functions, circuits, parallel computation models, function algebras, and proof systems. Its main aim is to elucidate the structure of "fast" ...

    Boolean RT.unitypackage,unity3d布尔运算插件

    Boolean RT.unitypackage,unity3d布尔运算插件。

    详谈java中boolean和Boolean的区别

    下面小编就为大家带来一篇详谈java中boolean和Boolean的区别。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

    BMS boolean

    saliency detection a boolean map approach

    Boolean_unity.zip

    用作unity3D几何体的布尔运算包用作unity3D几何体的布尔运算包用作unity3D几何体的布尔运算包

    boolean2:Boolean2 是一个 Ruby 常量,它是 true 和 false 的祖先

    下次要定义全局Boolean类之前,请考虑改用这种基本方法。 设置 添加到您的Gemfile : gem 'boolean2' 用法 true . is_a? Boolean2 #=&gt; true false . is_a? Boolean2 #=&gt; true nil . is_a? Boolean2 #=&gt; false ...

    javascript之Boolean类型对象

    Boolean对象和Java中的Boolean封装类很像,它有两个值:true和false 1、创建Boolean对象 复制代码 代码如下:var boo = new Boolean();//此时未对boo进行赋值,但是它的默认值为false var boo = new Boolean(true);...

Global site tag (gtag.js) - Google Analytics