Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

代码bug #22

Open
QAQ516284797 opened this issue Mar 24, 2022 · 2 comments
Open

代码bug #22

QAQ516284797 opened this issue Mar 24, 2022 · 2 comments

Comments

@QAQ516284797
Copy link

@Override
public List<Point> recordHandlePoints(List<ImportDataStep> steps) {

    System.out.println("多线程查询======== start");

    List<Point> points = new ArrayList<>();
    for (ImportDataStep step : steps) {
        Future<List<Point>> future = executorService.submit(new HighImportDataPointFuture(step, pointLogic));
        try {
            points.addAll(future.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }

    System.out.println("多线程查询======== point " + points.size() + " 条数==============");
    return points;
}

位于 HighImportDataServiceImpl 实现错误了吧,future.get()会阻塞线程 所以实际上是串行提交,没有线程并行
future.get()应该在任务提交之外的循环遍历获取吧

@QAQ516284797
Copy link
Author

@Override
public List<Point> recordHandlePoints(List<ImportDataStep> steps) {

    System.out.println("多线程查询======== start");

    List<Point> points = new ArrayList<>();
    LinkedList<Future<List<Point>>> tasks=new LinkedList<>();
    for (ImportDataStep step : steps) {
        tasks.add(executorService.submit(new HighImportDataPointFuture(step, pointLogic)));

    }
    while (!tasks.isEmpty()){
        try {
            points.addAll(tasks.pollFirst().get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }

    System.out.println("多线程查询======== point " + points.size() + " 条数==============");
    return points;
}

建议修改代码

@liujiawinds
Copy link

future替换成completablefuture之后,改使用allof才是正解.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants