获取每个executorservice线程后面的查询

时间:2015-12-30 08:17:10

标签: java multithreading executorservice

我在JAVA中使用executorsevice来执行一些线程,让我们说十个线程,线程数可能会有所不同。每个线程都在执行SQL Server查询。我正在使用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 个答案:

答案 0 :(得分:1)

备选方案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 );
}

然后迭代地图

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

}

备选方案3 如果要保留顺序,请创建一个使用Future和查询作为属性的类,然后将该类放在列表

   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);
        }
    }