我如何在此for循环中使用并行流

时间:2019-05-22 15:35:54

标签: java java-stream

jsonarray对象包含50000个jsonObject。我需要处理此jsonObject来创建对象,然后将其添加到列表中。这要花很多时间,所以我想使用并行流制作对象并更快地添加到列表中。

这是我要用并行循环替换的代码

        // I want to insert object in this array
        List<Curriculum> curriculamList = new ArrayList<>();

        // This is the org.json array
        JSONArray jsonarray = new JSONArray(content);

        // This loop I want to replace
        for (int i = 0; i < jsonarray.length(); i++) {
            JSONObject jsonobject = jsonarray.getJSONObject(i);

            // Start of processing
            Program program = programDAO.getProgramDetails(jsonobject.getInt("programId"));
            Batch batch = batchDAO.getBatchDetails(jsonobject.getInt("batchId"), program);
            if(batch.getBatchId() == 0)
                continue;
            Major major = majorDAO.getMajorDetails(jsonobject.getInt("majorId"));
            Course course = courseDAO.getCourseDetails(jsonobject.getString("courseCode"));
            if(course == null)
                continue;
            double credits = jsonobject.getDouble("credits");
            CourseType courseType = courseTypeDAO.getCourseTypeDetails(program, jsonobject.optString("type"));
            int semesterCount = jsonobject.getInt("semester");
            String prereqCourseString = jsonobject.getString("prereq");
            Course alternateCourse = courseDAO.getCourseDetails(jsonobject.getString("alternate"));

            List<Course> prereqCourseList = new ArrayList<>();
            if (prereqCourseString.length() != 0) {
                String[] prereqCourseSplit = prereqCourseString.split("AND");
                for (String prereqCourseSplitString : prereqCourseSplit) {
                    prereqCourseList.add(courseDAO.getCourseDetails(prereqCourseSplitString.trim()));
                }
            }
            List<Course> prereqChainCourseList = new ArrayList<>();
            // End of processing

            // This is the object
            Curriculum curriculum = new Curriculum(course, credits, courseType, semesterCount, prereqCourseList, prereqChainCourseList, alternateCourse, batch, major);

            // Pushing object into the list
            curriculamList.add(curriculum);
        }

我在这里尝试了一些代码,但可能是我走错了路。如何在流中使用列表并获取循环的索引号。请帮助我如何将其转换为流。

        JSONArray jsonarray = new JSONArray(content);
        Stream.of(jsonarray)
                .parallel()
                .forEach(objects -> {
                    // How I can get index number here and push it to the list?
                    JSONObject jsonobject = objects.getJSONObject(i);

                    // Here is the processing code

                    Curriculum curriculum = new Curriculum(course, credits, courseType, semesterCount, prereqCourseList, prereqChainCourseList, alternateCourse, batch, major);

                    // Variable used in lambda expression should be final or effectively final? How to add then?
                    curriculamList.add(curriculum);
                });

我是Java新手,如果遇到任何错误,请原谅我。

1 个答案:

答案 0 :(得分:5)

Stream.of(jsonarray)返回带有单个元素JSONArray对象的Stream,这不是您想要的。

我想这就是你要去的地方

List<Curriculum> curriculumList = IntStream.range(0, jsonarray.length()).parallel()
            .mapToObj(i -> {
                JSONObject jsonobject = jsonarray.getJSONObject(i);

                // fetch the various parts...

                return new Curriculum(course, credits, courseType, semesterCount, prereqCourseList, prereqChainCourseList, alternateCourse, batch, major);
            }).collect(Collectors.toList());

但是,您的代码似乎正在进行几次阻塞的网络调用。通常不建议使用并行流来并行执行阻塞的网络调用,因为并行流旨在用于CPU并行化,因此请使用只有少量线程的单个共享线程池。因此,您可能更喜欢这样:

ExecutorService executor = Executors.newFixedThreadPool(10);

// start many calls running in parallel
List<Future<Curriculum>> futures = IntStream.range(0, jsonarray.length())
        .mapToObj(i -> executor.submit(() -> {
                JSONObject jsonobject = jsonarray.getJSONObject(i);

                // fetch the various parts...

                return new Curriculum(course, credits, courseType, semesterCount, prereqCourseList, prereqChainCourseList, alternateCourse, batch, major);
            })).collect(Collectors.toList());

List<Curriculum> curriculumList = new ArrayList<>();
for (Future<Curriculum> future : futures) {
    curriculumList.add(future.get());
}

executor.shutdown();