如何通过返回Future取消异步触发的线程?

时间:2013-10-29 09:15:07

标签: java multithreading jsf asynchronous ejb-3.1

我花了几个小时研究我的问题,但我找到了解决方案。

我使用@Asynchronous注释触发一个线程并返回一个Future对象,到目前为止一直很好。但是,如果我试图取消未来,似乎该命令不会取消任何内容。

JSF文件

    <?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html">
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
        <h:form>
            <h:commandButton actionListener="#{threadHandler.start()}" value="start"/>
            <h:commandButton actionListener="#{threadHandler.stop()}" value="stop"/>
        </h:form>
    </h:body>
</html>

Java处理程序

    /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jsf.example;

import java.util.concurrent.Future;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.inject.Named;

@Named("threadHandler")
@Stateless
public class Handler {

    Future future;
    @Inject
    MethodPool pool;

    public void start(){
        System.out.println("start thread");
        future = pool.run();
        System.out.println("finish thread start");
    }

    public void stop(){
        System.out.println("future.cancel " + future.cancel(true));
    }
}

Java线程

 /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jsf.example;

import java.util.concurrent.Future;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.AsyncResult;
import javax.ejb.Asynchronous;
import javax.ejb.Stateless;


@Stateless
public class MethodPool {

    @Asynchronous
    public Future run(){
        System.out.println("start run");
        Integer runCondition = 1;
        while(runCondition > 0){
            try {
                // simulate heavy stuff
                Thread.sleep(1000);
                System.out.println(". ");
            } catch (InterruptedException ex) {
                Logger.getLogger(MethodPool.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
        return new AsyncResult("finish run");
    }

}

* 编辑:THX @bkail *

我将为下一个绝望的人发布工作代码示例;)

@Resource
    private SessionContext sessionContext;

@Asynchronous
    public Future run(){
        System.out.println("start run");
        while(sessionContext.wasCancelCalled() == false){
            try {
                // simulate heavy stuff
                Thread.sleep(1000);
                System.out.println(". ");
            } catch (InterruptedException ex) {
                Logger.getLogger(MethodPool.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
        return new AsyncResult("finish run");
    }

1 个答案:

答案 0 :(得分:4)

EJB使用协作取消,因此传递cancel(interrupt = true)实际上并不会中断。相反,它在方法调用上设置一个标志。在EJB中,使用@Resource SessionContext context;,然后检查context.wasCancelCalled()以检查是否调用了cancel(interrupt = true)。