java 如何为流操作创建线程池

ih99xse1  于 2023-04-10  发布在  Java
关注(0)|答案(1)|浏览(95)

我喜欢在线程池中使用流来控制线程的执行。

List<String> mylist = new ArrayList() {"1","2","3","4"}; //that holds the strings 
List<Action> actions = new ArrayList<>{} // holds function that manipulate the strings from mylist

每个动作都有从mylist中获取String的工作方法。

Stream<String> stream = mylist.parallelStream();
stream = stream.flatMap(s-> actions.stream().map(ac -> ac.work(str)));
r = stream.collect(Collectors.toList());

所有工作都很好,但我无法控制线程池,知道我可以使用ForkJoinPool在这个例子中:
Example
但是我没有找到在我的例子中实现它的方法。这个例子不起作用:

ForkJoinPool customThreadPool = new ForkJoinPool(4);
            r= customThreadPool.submit(
                    () -> mylist.parallelStream().flatMap(s-> actions.stream().map(ac -> ac.work(str))).collect(Collectors.toList()));

给我报错:

java: incompatible types: no instance(s) of type variable(s) T,R,A,capture#1 of ?,T exist so that java.util.concurrent.ForkJoinTask<T> conforms to java.util.List<java.lang.String>
n8ghc7c1

n8ghc7c11#

一旦代码错误被修复(str =〉s),代码就可以很好地编译和运行。

公共池

// Setup with dummy actions for testing which thread executes the action
List<String> mylist = new ArrayList<>(Arrays.asList("1","2","3","4")); //that holds the strings 
List<Action> actions = new ArrayList<>(Arrays.asList(
        s -> { s += "x";  System.out.println(Thread.currentThread().getName() + ": " + s); return s; },
        s -> { s += "y";  System.out.println(Thread.currentThread().getName() + ": " + s); return s; }
        ));

// Using common pool
Stream<String> stream = mylist.parallelStream();
stream = stream.flatMap(s -> actions.stream().map(ac -> ac.work(s)));
List<String> r = stream.collect(Collectors.toList());
System.out.println(r);
  • 输出 *
ForkJoinPool.commonPool-worker-7: 1x
ForkJoinPool.commonPool-worker-3: 2x
ForkJoinPool.commonPool-worker-3: 2y
main: 3x
ForkJoinPool.commonPool-worker-5: 4x
main: 3y
ForkJoinPool.commonPool-worker-7: 1y
ForkJoinPool.commonPool-worker-5: 4y
[1x, 1y, 2x, 2y, 3x, 3y, 4x, 4y]

自定义池

ForkJoinPool customThreadPool = new ForkJoinPool(4);
ForkJoinTask<List<String>> task = customThreadPool.submit(
        () -> mylist.parallelStream().flatMap(s -> actions.stream().map(ac -> ac.work(s))).collect(Collectors.toList()));
System.out.println(task.get());

如果编译器像问题中描述的那样抱怨,你需要通过在第三行转换lambda表达式来帮助它选择正确的submit()重载:

(Callable<List<String>>) () -> mylist.parallelStream().flatMap(s -> actions.stream().map(ac -> ac.work(s))).collect(Collectors.toList()));
  • 输出 *
ForkJoinPool-1-worker-3: 3x
ForkJoinPool-1-worker-1: 1x
ForkJoinPool-1-worker-1: 1y
ForkJoinPool-1-worker-5: 2x
ForkJoinPool-1-worker-7: 4x
ForkJoinPool-1-worker-7: 4y
ForkJoinPool-1-worker-5: 2y
ForkJoinPool-1-worker-3: 3y
[1x, 1y, 2x, 2y, 3x, 3y, 4x, 4y]

单线程

Stream<String> stream = mylist.stream();
stream = stream.flatMap(s -> actions.stream().map(ac -> ac.work(s)));
List<String> r = stream.collect(Collectors.toList());
System.out.println(r);
  • 输出 *
main: 1x
main: 1y
main: 2x
main: 2y
main: 3x
main: 3y
main: 4x
main: 4y
[1x, 1y, 2x, 2y, 3x, 3y, 4x, 4y]

相关问题