5
Tôi đang cố gắng để chạy nhiều nhiệm vụ theo lịch trình cùng lúc khi khởi động vào mùa xuân, nhưng trong thực tế họ chạy xếp hàng (một sau khi khác, không song song)Nhiều nhiệm vụ xuân @Scheduled đồng thời
Đây là dịch vụ đơn giản của tôi :
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
@Service
public class MyScheduleDemo {
@Scheduled(fixedDelay = 5000, initialDelay = 1000)
public void taskA() throws InterruptedException {
System.out.println("[A] Starting new cycle of scheduled task");
// Simulate an operation that took 5 seconds.
long startTime = System.currentTimeMillis();
while (System.currentTimeMillis() - startTime <= 5000);
System.out.println("[A] Done the cycle of scheduled task");
}
@Scheduled(fixedDelay = 5000, initialDelay = 2000)
public void taskB() throws InterruptedException {
System.out.println("[B] Starting new cycle of scheduled task");
System.out.println("[B] Done the cycle of scheduled task");
}
}
Output:
[A] Starting new cycle of scheduled task
[A] Done the cycle of scheduled task
[B] Starting new cycle of scheduled task
[B] Done the cycle of scheduled task
Nhưng, Nó phải như:
[A] Starting new cycle of scheduled task
[B] Starting new cycle of scheduled task
[B] Done the cycle of scheduled task
[A] Done the cycle of scheduled task
Tôi đang làm gì sai?
Đây là cấu hình của tôi:
@Configuration
@EnableAsync
@EnableScheduling
public class AsyncConfiguration implements AsyncConfigurer {
@Override
@Bean(name = "taskExecutor")
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(6);
executor.setMaxPoolSize(50);
executor.setQueueCapacity(100);
executor.setThreadNamePrefix("customer-Executor-");
executor.initialize();
return executor;
}
}
Bạn đang bối rối 'TaskExecutor' với' TaskScheduler' bạn chưa định cấu hình và sau đó mọi thứ chạy trong chế độ đồng bộ (mặc định). –
Cảm ơn @ M.Deinum !! – scheduleds