gpt4 book ai didi

java - 获取每个 executorservice 线程背后的查询

转载 作者:搜寻专家 更新时间:2023-11-01 03:35:04 24 4
gpt4 key购买 nike

我在 JAVA 中使用 executorsevice 来执行一些线程,假设十个线程,线程数可能会有所不同。每个线程都在执行一个 SQL 服务器查询。我正在使用 Future 和 Callable 类来提交任务。每个线程完成后,我都会[使用 future.get()] 得到结果。

现在我的要求是我需要知道每个线程在返回结果后执行的查询,即使结果是一个空集也是如此。

这是我的代码:

List<Future<List>> list = new ArrayList<Future<List>>();
int totalThreads = allQueriesWeight.size();
ExecutorService taskExecutor = Executors.newFixedThreadPool(totalThreads);
for (String query : allQueriesWeight) {//allQueriesWeight is an arraylist containing sql server queries
SearchTask searchTask = new SearchTask(query);
Future<List> submit = taskExecutor.submit(searchTask);
list.add(submit);
}

这是我的调用函数:

@Override
public List<SearchResult> call() throws Exception {
java.sql.Statement statement = null;
Connection co = null;
List<SearchResult> allSearchResults = new ArrayList();
try {
//executing query and getting results
while (r1.next()) {
...
allSearchResults.add(r);//populating array
}
} catch (Exception e) {
Logger.getLogger(GenericResource.class.getName()).log(Level.SEVERE, null, e);
} finally {
if (statement != null) {
statement.close();
}
if (co != null) {
co.close();
}
}
return allSearchResults;
}

这是我得到结果的方式:

for (Future<List> future : list) {
try {
System.out.println(future.get().size());
List<SearchResult> sr = future.get();
} catch (InterruptedException ex) {
Logger.getLogger(GenericResource.class.getName()).log(Level.SEVERE, null, ex);
} catch (ExecutionException ex) {
Logger.getLogger(GenericResource.class.getName()).log(Level.SEVERE, null, ex);
}
}

在上面的 for 循环中,我需要确定返回结果的查询。我是新手,非常感谢任何帮助/建议。

谢谢。

最佳答案

备选方案 1:您拥有相同顺序和相同大小的两个列表,因此您可以简单地执行以下操作

for (int i = 0; i < allQueriesWeight.size(); i++) {
allQueriesWeight.get(i);
futureList.get(i);
}

备选方案 2:如果所有查询都不同,您可以使用如下所示的映射,但这种方法会丢失执行顺序。

int totalThreads = allQueriesWeight.size();
Map<String,Future<List>> map = new HashMap<>;
ExecutorService taskExecutor = Executors.newFixedThreadPool(totalThreads);
for (String query : allQueriesWeight) {//allQueriesWeight is an arraylist containing sql server queries
SearchTask searchTask = new SearchTask(query);
Future<List> submit = taskExecutor.submit(searchTask);
map.put(query ,submit );
}

然后迭代 map

for (Entry<String,Future<List>> future : map.) {
System.out.println("query is:" +map.getKey());
List<SearchResult> sr = map.getValue().get();

}

备选方案 3如果要保持顺序,请创建一个以 Future 和 query 作为属性的类,然后将该类放入列表中

   public class ResultWithQuery {

private final Future<List<?>> future;
private final String query;


public ResultWithQuery(Future<List<?>> future, String query) {
this.future = future;
this.query = query;
}


public Future<List<?>> getFuture() {
return future;
}


public String getQuery() {
return query;
}

}

List<ResultWithQuery > list = new ArrayList<ResultWithQuery >();
int totalThreads = allQueriesWeight.size();
ExecutorService taskExecutor = Executors.newFixedThreadPool(totalThreads);
for (String query : allQueriesWeight) {//allQueriesWeight is an arraylist containing sql server queries
SearchTask searchTask = new SearchTask(query);
Future<List> submit = taskExecutor.submit(searchTask);
list.add(new ResultWithQuery (submit, query));
}

并迭代列表

for (ResultWithQuery resQuery: list) {
try {
resQuery.getQuery();
List<SearchResult> sr = resQuery.getFuture.get();
} catch (InterruptedException ex) {
Logger.getLogger(GenericResource.class.getName()).log(Level.SEVERE, null, ex);
} catch (ExecutionException ex) {
Logger.getLogger(GenericResource.class.getName()).log(Level.SEVERE, null, ex);
}
}

关于java - 获取每个 executorservice 线程背后的查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34525539/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com