![](/img/trans.png)
[英]How to run the same @Scheduled method in Spring Boot in multiple threads
[英]How to run multiple long task method in the same time in Spring Boot Service?
这是我的问题,我创建了一个服务 class 处理从 output 文件夹上传文件到云。 在这个output文件夹内,触发时会产生大量文件。
下面是我的 class 的服务。
@Service
Public class uploadService {
public void UploadAllFile(String processCode) {
if (processCode = "allfile") {
1 //no need check generate status, start to upload while files generating
//keep upload files while generate process ongoing, until finish generate.
uploadPDF();
2 //check other type of files generate status, wait all finish just start upload.
ReadFileGenerateLog;
if (log.contain("txt") == "all finish generate") {
uploadTxt();
}
if (log.contain("xml") == "all finish generate") {
uploadXml();
}
}
}
public void UploadPDF(){
}
public void UploadTxt(){
}
public void UploadXml(){
}
}
现在的问题是需要生成和上传几百个 PDF 文件,完成整个过程需要 1 小时 smtg(上传时仍然生成直到完成)。 而其他类型的文件在 10 分钟内完成(等到完成生成和上传)。
所以根据我的代码逻辑,不可能等待 [1] function 完成然后开始 [2] function。 无论如何,我可以同时运行两个逻辑而不是等待第一个逻辑完成吗? 请教我解决这个问题。
ExecutorService executor = Executors.newFixedThreadPool(10);
for(int i = 0; i < FILE_COUNT; i++) {
executor.submit(() -> {
uploadPDF(i)
})
}
只需使用 ExecutorService 管理多线程上传 PDF。 你也可以得到结果如果你调查https://www.baeldung.com/java-executor-service-tutorial
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.