首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >同时使用注解@Asynchronous的两个方法

同时使用注解@Asynchronous的两个方法
EN

Stack Overflow用户
提问于 2015-12-24 06:05:58
回答 1查看 70关注 0票数 0

我正在开发一个Web服务应用程序(JDK 1.7)。我想使用@Asynchronous注解同时执行两个方法。我的代码描述如下:

代码语言:javascript
复制
@Stateless
@Asynchronous
public class One {

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    @Asynchronous
    public Future<String> doSomething1(String arg1) {
        return new AsyncResult(arg1);
    }
}


@Stateless
@Asynchronous
public class Two {

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    @Asynchronous
    public Future<String> doSomething2(String arg2) {
        return new AsyncResult(arg2);
    }
}

public class OnePlusTwo {   

    private String operation1;
    private String operation2;

    /**
     * @return the operation1
     */
    public String getOperation1() {
        return operation1;
    }

    /**
     * @param operation1 the operation1 to set
     */
    public void setOperation1(String operation1) {
        this.operation1 = operation1;
    }

    /**
     * @return the operation2
     */
    public String getOperation2() {
        return operation2;
    }

    /**
     * @param operation2 the operation2 to set
     */
    public void setOperation2(String operation2) {
        this.operation2 = operation2;
    }
}

@Stateless
public class WholeOperation {

    @EJB
    One one;

    @EJB
    Two two;

    public OnePlusTwo getOnePlusTwo() {
        ExecutorService executorService = Executors.newFixedThreadPool(2);
        OnePlusTwo onePlusTwo = new OnePlusTwo();
        //WHAT TO DO HERE????
        executorService.execute(onePlusTwo.setOperation1(one.doSomething1("at").get()));
        executorService.execute(onePlusTwo.setOperation2(two.doSomething2("at same time").get()));
        executorService.shutdown();
        return onePlusTwo;
    }

}

我哪里做错了?如何执行此操作?

EN

回答 1

Stack Overflow用户

发布于 2015-12-24 06:32:21

您在这里有几个地方是错的。

如果要调用@方法,则不需要使用ExecutorService。容器为您管理这些东西;,

  1. ,您正在对@Asynchronous方法返回的未来调用get方法。get方法会一直阻塞,直到异步完成未来。通常,直到真正需要该值时才调用Future.get(),因为在此期间可以执行许多其他处理。

试一试

代码语言:javascript
复制
public class OnePlusTwo {   

    private final Future<String> operation1;
    private final Future<String> operation2;

    public OnePlusTwo(Future<String> operation1, Future<String> operation2) {
        this.operation1 = operation1;
        this.operation2 = operation2;           
    }

    /**
     * @return the operation1
     */
    public String getOperation1() {
        return operation1.get();
    }

    /**
     * @return the operation2
     */
    public String getOperation2() {
        return operation2.get();
    }

}

@Stateless
public class WholeOperation {

    @EJB
    One one;

    @EJB
    Two two;

    public OnePlusTwo getOnePlusTwo() {

        Future<String> f1 = one.doSomething1("at");
        Future<String> f2 = two.doSomething2("at same time");

        OnePlusTwo onePlusTwo = new OnePlusTwo(f1, f2);

        return onePlusTwo;
    }

}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34444447

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档