Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java 8中,如何将play.libs.concurrent.HttpExecutionContext传递给并行流?_Java_Playframework_Stream - Fatal编程技术网

在Java 8中,如何将play.libs.concurrent.HttpExecutionContext传递给并行流?

在Java 8中,如何将play.libs.concurrent.HttpExecutionContext传递给并行流?,java,playframework,stream,Java,Playframework,Stream,在play framework项目控制器中,我正在使用forEach()处理对象列表,这很好 List<Post> posts = repository.getPosts(); posts.forEach(post -> { //...some processing anyFunc(); //<-- internally uses HttpExecutionContext //...further processing }); List po

在play framework项目控制器中,我正在使用forEach()处理对象列表,这很好

List<Post> posts = repository.getPosts();
posts.forEach(post -> {
    //...some processing

    anyFunc(); //<-- internally uses HttpExecutionContext

    //...further processing
});
List posts=repository.getPosts();
posts.forEach(post->{
//…一些处理
anyFunc();//{
//…一些处理

anyFunc();//使用
HttpExecutionContext.execute

public class HomeController extends Controller {
    @Inject HttpExecutionContext ec;

    public Result index() {
        // The data to parallel processing
        List<String> list = List.of("Item 1", "Item 2", "Item 3","Item 4", "Item 5", "Item 6", "Item 7", "Item 8");

        // Make a Stream. The `parallelStream` is not used because 
        // `current.execute` will make it run in parallel.
        Stream<String> listInParralel = list.stream(); 

        // The current executor with the HTTP context. 
        Executor current = ec.current();

        System.out.println("START");
        listInParralel.forEach(item -> {
          current.execute(()-> {
            // request().uri() internally uses HttpExecutionContext
            System.out.println("item: " + item + " in "  +  request().uri()  + "(" + Thread.currentThread().getName() + ")");
          });
        });

        // Results
        /*
        START
        item: Item 7 in /(application-akka.actor.default-dispatcher-9)
        item: Item 5 in /(application-akka.actor.default-dispatcher-7)
        item: Item 3 in /(application-akka.actor.default-dispatcher-5)
        item: Item 1 in /(application-akka.actor.default-dispatcher-6)
        item: Item 6 in /(application-akka.actor.default-dispatcher-8)
        item: Item 4 in /(application-akka.actor.default-dispatcher-2)
        item: Item 2 in /(application-akka.actor.default-dispatcher-4)
        item: Item 8 in /(application-akka.actor.default-dispatcher-9)
        */

        return ok("Done");
    }

}
能否请您分享一份报告,以便我们复制您的问题?
public class HomeController extends Controller {
    @Inject HttpExecutionContext ec;

    public Result index() {
        // The data to parallel processing
        List<String> list = List.of("Item 1", "Item 2", "Item 3","Item 4", "Item 5", "Item 6", "Item 7", "Item 8");

        // Make a Stream. The `parallelStream` is not used because 
        // `current.execute` will make it run in parallel.
        Stream<String> listInParralel = list.stream(); 

        // The current executor with the HTTP context. 
        Executor current = ec.current();

        System.out.println("START");
        listInParralel.forEach(item -> {
          current.execute(()-> {
            // request().uri() internally uses HttpExecutionContext
            System.out.println("item: " + item + " in "  +  request().uri()  + "(" + Thread.currentThread().getName() + ")");
          });
        });

        // Results
        /*
        START
        item: Item 7 in /(application-akka.actor.default-dispatcher-9)
        item: Item 5 in /(application-akka.actor.default-dispatcher-7)
        item: Item 3 in /(application-akka.actor.default-dispatcher-5)
        item: Item 1 in /(application-akka.actor.default-dispatcher-6)
        item: Item 6 in /(application-akka.actor.default-dispatcher-8)
        item: Item 4 in /(application-akka.actor.default-dispatcher-2)
        item: Item 2 in /(application-akka.actor.default-dispatcher-4)
        item: Item 8 in /(application-akka.actor.default-dispatcher-9)
        */

        return ok("Done");
    }

}
public class HomeController extends Controller {
    @Inject HttpExecutionContext ec;

    public Result index() {
        // The data to parallel processing
        List<String> list = List.of("Item 1", "Item 2", "Item 3","Item 4", "Item 5", "Item 6", "Item 7", "Item 8");
        Stream<String> listInParralel = list.parallelStream(); 

        // Take all that you need from the HttpExecutionContext.  
        String uri = request().uri();

        System.out.println("START");
        listInParralel.forEach(item -> {
            // use pre cached HTTP context data, liek `uri`
            System.out.println("item: " + item + " in "  +  uri  + "(" + Thread.currentThread().getName() + ")");
        });

        // Results
        /*
        START
        item: Item 1 in /(ForkJoinPool.commonPool-worker-7)
        item: Item 8 in /(ForkJoinPool.commonPool-worker-3)
        item: Item 7 in /(ForkJoinPool.commonPool-worker-15)
        item: Item 4 in /(ForkJoinPool.commonPool-worker-9)
        item: Item 3 in /(ForkJoinPool.commonPool-worker-13)
        item: Item 2 in /(ForkJoinPool.commonPool-worker-5)
        item: Item 5 in /(ForkJoinPool.commonPool-worker-11)
        item: Item 6 in /(application-akka.actor.default-dispatcher-4)
        */

        return ok("Done");
    }

}