使用Spring Boot的多线程功能。

huangapple 未分类评论50阅读模式
英文:

using mulit thread with spring boot

问题

我正在使用Spring Boot开发一个项目。
如果我创建一个类像这样,线程不起作用。
DAO按顺序工作。
示例(1.2.3.4.5.....100)

服务类
    for(int i = 1; i <= 30; j++) {
      threadServiceMethod(i);
    }

ThreadService类
    @Async
    public CompletableFuture<List<DTO>> threadServiceMethod(int i) {
            return CompletableFuture.completedFuture(DAO方法);
    }

DAO类
    public List<Dto> daoMethod() {
        //选择代码
    }

但是,如果将@Async连接到daoMethod,它会起作用。
但是,客户端接收到的结果值是null。

ThreadService类
    public CompletableFuture<List<DTO>> threadServiceMethod(int i) {
            return CompletableFuture.completedFuture(DAO方法);
    }

DAO类

    @Async
    public List<Dto> daoMethod() {
        //选择代码
    }

如果对threadmethod和dao使用@Async,日志将按顺序输出。
为什么会这样工作呢?
帮帮我 使用Spring Boot的多线程功能。

英文:

I am working on a project using Spring Boot.
If I create a class like this the thread doesn't work.
DAO works sequentially.
example (1.2.3.4.5.....100)

Service class
    for(int i = 1; i &lt;= 30; j++) {
      threadServiceMethod(i);
    }

ThreadService class
    @Async
    public CompletableFuture&lt;List&lt;DTO&gt;&gt; threadServiceMethod(int i) {
            return CompletableFuture.completedFuture(DAO method);
    }

DAO class
    public List&lt;Dto&gt; daoMethod() {
        //select code
    }

But It works if @Async is connected to daoMethod
However, the result value that the client receives is null.

ThreadService class
    public CompletableFuture&lt;List&lt;DTO&gt;&gt; threadServiceMethod(int i) {
            return CompletableFuture.completedFuture(DAO method);
    }

DAO class

    @Async
    public List&lt;Dto&gt; daoMethod() {
        //select code
    }

If @async is used for threadmethod and dao, logs are output sequentially.
Why does it work like this??
help me 使用Spring Boot的多线程功能。

huangapple
  • 本文由 发表于 2020年7月24日 08:06:31
  • 转载请务必保留本文链接:https://java.coder-hub.com/63064897.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定