多线程在Spring Boot中

huangapple go评论83阅读模式
英文:

Multithreading in Spring Boot

问题

我对Spring完全不熟悉。尝试使用Spring Async注解在单独的线程上调用方法。这是我在大致查阅后尝试的代码:

public class MyClass {

    private static final Logger LOGGER = LoggerFactory.getLogger(MyClass.class);

    @Async("threadPoolTaskExecutor")
    public void asyncMethodWithVoidReturnType() {
        System.out.println("Hello from sout! Execute method asynchronously. " + Thread.currentThread().getName());
        LOGGER.info("Hello from logger! Execute method asynchronously. " + Thread.currentThread().getName());
    }

}

以及运行程序的类:

@SpringBootApplication
@EnableAsync
public class SpringTestRunner {

    @Bean("threadPoolTaskExecutor")
    public Executor getAsyncExecutor() {
        return new ThreadPoolTaskExecutor();
    }

    public static void main(String[] args) {
        SpringApplication.run(SpringTestRunner.class, args);
    }
}

当运行主类时,我没有看到来自线程的任何输出。为什么会这样呢?
正如您所看到的,这是一个非常基础的问题,请尽量详细解释。

英文:

I am completely new to Spring. Trying to call a method on a separate thread using Spring Async annotation. This is the code I have tried after looking around a bit:

public class MyClass {

    private static final Logger LOGGER = LoggerFactory.getLogger(MyClass.class);

    @Async("threadPoolTaskExecutor")
    public void asyncMethodWithVoidReturnType() {
        System.out.println("Hello from sout! Execute method asynchronously. " + Thread.currentThread().getName());
        LOGGER.info("Hello from logger! Execute method asynchronously. " + Thread.currentThread().getName());
    }

}

and the runner:

@SpringBootApplication
@EnableAsync
public class SpringTestRunner {

    @Bean("threadPoolTaskExecutor")
    public Executor getAsyncExecutor() {
        return new ThreadPoolTaskExecutor();
    }

    public static void main(String[] args) {
        SpringApplication.run(SpringTestRunner.class, args);
    }
}

When running the main class, I do not see any output from the thread. Why is that?
As you can tell, this is a very basic question, so please explain as much as you can.

答案1

得分: 0

以下是您要翻译的内容:

您没有看到输出是因为您没有调用asyncMethodWithVoidReturnType函数。使用调度器的示例:

写入者(Writer):

@Slf4j
@Service
public class AsyncWriter {
    @Async("threadPoolTaskExecutor")
    @SneakyThrows
    public void asyncWrite() {
        log.info("Hello! " + Thread.currentThread().getName());
        Thread.sleep(1000);
    }
}

调度器(Scheduler):

@Slf4j
@Service
@RequiredArgsConstructor
public class Scheduler {

    private final AsyncWriter asyncWriter;

    @Scheduled(fixedDelay = 1000)
    public void scheduledWrite() {
        log.info("Scheduler");
        for(int i = 0; i < 10; i++) {
            asyncWriter.asyncWrite();
        }
    }
}

运行程序(Runner):

@SpringBootApplication
@EnableAsync
@EnableScheduling
public class SpringTestRunner {

    @Bean("threadPoolTaskExecutor")
    public Executor getAsyncExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(10);
        return executor;
    }

    public static void main(String[] args) {
        SpringApplication.run(SpringTestRunner.class, args);
    }
}
英文:

You don't see output because you don't call your asyncMethodWithVoidReturnType function. Example with scheduler:

Writer:

@Slf4j
@Service
public class AsyncWriter {
    @Async(&quot;threadPoolTaskExecutor&quot;)
    @SneakyThrows
    public void asyncWrite() {
        log.info(&quot;Hello! &quot; + Thread.currentThread().getName());
        Thread.sleep(1000);
    }
}

Scheduler:

@Slf4j
@Service
@RequiredArgsConstructor
public class Scheduler {

    private final AsyncWriter asyncWriter;

    @Scheduled(fixedDelay = 1000)
    public void scheduledWrite() {
        log.info(&quot;Scheduler&quot;);
        for(int i = 0; i &lt; 10; i++) {
            asyncWriter.asyncWrite();
        }
    }
}

Runner:

@SpringBootApplication
@EnableAsync
@EnableScheduling
public class SpringTestRunner {

    @Bean(&quot;threadPoolTaskExecutor&quot;)
    public Executor getAsyncExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(10);
        return executor;
    }

    public static void main(String[] args) {
        SpringApplication.run(SpringTestRunner.class, args);
    }
}

huangapple
  • 本文由 发表于 2020年10月18日 01:29:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/64405342.html
匿名

发表评论

匿名网友

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

确定