英文:
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("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);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论