Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在循环中为每次迭代使用executor服务? private ExecutorService ExecutorService=Executors.newFixedThreadPool(3); 公共方法(){ int batchSize=2; //需要3页来测试 对于(int i=0;i{ pageNum[0]=pageNum[0]+1; fetchDataAndPushEvents(pageNum[0],batchSize); }); } } 私有void fetchDataAndPushEvents(int pageNum、int batchSize){ log.info(“获取页码:{}和批大小:{}的数据”,pageNum,batchSize); long startTime=System.currentTimeMillis(); Pageable Pageable=PageRequest.of(pageNum,batchSize); 列表结果=myService.getAllResponse(可分页).getContent(); long secondTime=System.currentTimeMillis(); log.info(“获取页码:{}和批大小:{}的数据所用的时间为:{}”,pageNum,batchSize,secondTime startTime); 如果(!CollectionUtils.isEmpty(结果)){ info(“为页码:{}和批大小:{}推送事件”,pageNum,batchSize); 事件(结果); log.info(“为页码:{}和批大小:{}推送事件所花费的时间为:{}”,pageNum,batchSize,System.currentTimeMillis()-secondTime); }否则{ //终止进程 返回; } }_Java_Multithreading_Executorservice - Fatal编程技术网

Java 如何在循环中为每次迭代使用executor服务? private ExecutorService ExecutorService=Executors.newFixedThreadPool(3); 公共方法(){ int batchSize=2; //需要3页来测试 对于(int i=0;i{ pageNum[0]=pageNum[0]+1; fetchDataAndPushEvents(pageNum[0],batchSize); }); } } 私有void fetchDataAndPushEvents(int pageNum、int batchSize){ log.info(“获取页码:{}和批大小:{}的数据”,pageNum,batchSize); long startTime=System.currentTimeMillis(); Pageable Pageable=PageRequest.of(pageNum,batchSize); 列表结果=myService.getAllResponse(可分页).getContent(); long secondTime=System.currentTimeMillis(); log.info(“获取页码:{}和批大小:{}的数据所用的时间为:{}”,pageNum,batchSize,secondTime startTime); 如果(!CollectionUtils.isEmpty(结果)){ info(“为页码:{}和批大小:{}推送事件”,pageNum,batchSize); 事件(结果); log.info(“为页码:{}和批大小:{}推送事件所花费的时间为:{}”,pageNum,batchSize,System.currentTimeMillis()-secondTime); }否则{ //终止进程 返回; } }

Java 如何在循环中为每次迭代使用executor服务? private ExecutorService ExecutorService=Executors.newFixedThreadPool(3); 公共方法(){ int batchSize=2; //需要3页来测试 对于(int i=0;i{ pageNum[0]=pageNum[0]+1; fetchDataAndPushEvents(pageNum[0],batchSize); }); } } 私有void fetchDataAndPushEvents(int pageNum、int batchSize){ log.info(“获取页码:{}和批大小:{}的数据”,pageNum,batchSize); long startTime=System.currentTimeMillis(); Pageable Pageable=PageRequest.of(pageNum,batchSize); 列表结果=myService.getAllResponse(可分页).getContent(); long secondTime=System.currentTimeMillis(); log.info(“获取页码:{}和批大小:{}的数据所用的时间为:{}”,pageNum,batchSize,secondTime startTime); 如果(!CollectionUtils.isEmpty(结果)){ info(“为页码:{}和批大小:{}推送事件”,pageNum,batchSize); 事件(结果); log.info(“为页码:{}和批大小:{}推送事件所花费的时间为:{}”,pageNum,batchSize,System.currentTimeMillis()-secondTime); }否则{ //终止进程 返回; } },java,multithreading,executorservice,Java,Multithreading,Executorservice,我的目标是获取上述代码中提到的批大小为2(假设)和3(假设)批/页的数据。我试图赋予获取数据并将一个页面的事件推送到一个线程的责任,即一个页面一个线程。我做了这件事,能够写出上面的逻辑 但是,当我试图运行上述代码时,我从getAllResponse存储库调用中得到了与所有三次迭代相同的2个响应(与批大小相同)。pageNum未更新。获取第0页的所有响应,因此在所有3次迭代中推送相同的事件。我应该如何处理这个问题? PS:我对executorService不太了解。您没有增加pageNum,它对每

我的目标是获取上述代码中提到的批大小为2(假设)和3(假设)批/页的数据。我试图赋予获取数据并将一个页面的事件推送到一个线程的责任,即一个页面一个线程。我做了这件事,能够写出上面的逻辑

但是,当我试图运行上述代码时,我从
getAllResponse
存储库调用中得到了与所有三次迭代相同的2个响应(与批大小相同)。
pageNum
未更新。获取第0页的所有响应,因此在所有3次迭代中推送相同的事件。我应该如何处理这个问题?
PS:我对executorService不太了解。

您没有增加pageNum,它对每个迭代都有相同的值。 您必须为它使用单独的计数器变量,如下所示

private ExecutorService executorService = Executors.newFixedThreadPool(3);

public void myMethod(){
    int batchSize = 2;
    // calling for 3 pages to test
    for(int i=0;i<3; i++) {
        int[] pageNum = {-1};
        executorService.execute(() -> {
            pageNum[0] = pageNum[0] + 1;
            fetchDataAndPushEvents(pageNum[0], batchSize);  
        }); 
    }
}
private void fetchDataAndPushEvents(int pageNum, int batchSize) {
    log.info("Fetching data for page number: {} and batch size: {}", pageNum, batchSize);
        
    long startTime = System.currentTimeMillis();
    Pageable pageable = PageRequest.of(pageNum, batchSize);
    List<AnswerDto> result = myService.getAllResponse(pageable).getContent();
    long secondTime = System.currentTimeMillis();
    log.info("Time taken to fetch data for page number: {} and batch size: {} is :{}", pageNum, batchSize, secondTime-startTime);

    if(!CollectionUtils.isEmpty(result)) {
        log.info("Pushing events for page number: {} and batch size: {}", pageNum, batchSize);
        pushSomeEvent(result);
        log.info("Time taken to push event for page number: {} and batch size: {} is :{}", pageNum, batchSize, System.currentTimeMillis()-secondTime);
        } else {
            // to terminate the process
            return;
        }
    }
intpagenum=0;
对于(int i=0,pageCounter=0;i{
fetchDataAndPushEvents(pageNum+pageCounter,batchSize);
}); 
}

谢谢你指出,我完全忘了。另外,你能告诉我我是否使用无限while循环
while(true){}
而不是上面的有限while循环
for
loop,当
结果
列表为空时,我应该如何退出/关闭执行器服务?在while循环中添加条件,并在成功存在后使用break调用执行器服务的shutdown/shutdownnow方法。
 int pageNum = 0; 
 for(int i=0, pageCounter=0;i<3; i++,pageCounter++) {
    executorService.execute(() -> {
        fetchDataAndPushEvents(pageNum + pageCounter, batchSize);  
    }); 
}