简体   繁体   English

异步无法在游戏中运行2.1

[英]Async not working in play 2.1

I have been wanting to test the async methods. 我一直想测试异步方法。 I just couldn't understand - for every async that is called, is a new thread assigned to it or do they go into a queue in one thread. 我只是不明白-对于每个被调用的异步,是分配给它的新线程还是它们进入一个线程的队列。

So wanted to test it but got strange results. 因此想对其进行测试,但得到了奇怪的结果。 code: 码:

    // block request and current thread
    public static Result syncFreeze() {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                System.in));
        String a = null;
        try {
            System.out.println("sync freeze " + Thread.activeCount()
                    + " threads running with active thread name "
                    + Thread.currentThread().getName());
            a = reader.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ok(a);
    }

    // block request but should keep thread open
    public static Result asyncFreeze() {
        Promise<String> promiseOfString = play.libs.Akka.future(new Reader());
        return async(promiseOfString.map(new Function<String, Result>() {
            public Result apply(String s) {
                return ok(s);
            }
        }));
    }

    static class Reader implements Callable<String> {
        public String call() {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    System.in));
            String a = null;
            System.out.println("Async freeze " + Thread.activeCount()
                    + " threads running with active thread name "
                    + Thread.currentThread().getName());
            try {
                a = reader.readLine();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return a;
        }
    }

I thought that if the asyncFreeze was called, that this would never block the thread handling http requests. 我以为如果调用asyncFreeze,它将永远不会阻塞处理http请求的线程。 But it does not perform any differently then syncFreeze(). 但是它的执行与syncFreeze()并没有什么不同。 For both methods, - once called eight times, no other page can be loaded - showing that all threads serving requests have been blocked. 对于这两种方法,一次调用八次,无法加载其他页面。这表明服务请求的所有线程均已被阻止。 But this does not make sense to me - that was supposed to be the whole point of the async() method. 但这对我来说没有意义-那应该是async()方法的重点。 It should allow the main thread handling requests to continue. 它应该允许主线程处理请求继续进行。 All talked about here . 所有人都在这里谈论。

What am I doing wrong? 我究竟做错了什么?

Thanks a lot 非常感谢

(Suggested Edit) - how I tested it: (建议编辑)-我如何测试它:

I simply reloaded the browser directed to the sync and async methods. 我只是重新加载了针对sync和async方法的浏览器。 For every call the method locks until input from terminal -. 对于每次调用,该方法都会锁定,直到从终端-输入。 At first I do not unblock it. 首先,我不会取消阻止它。 I simply keep reloading the call to sync() or async() whilst in another browser window I try to load an index() page. 我只是继续重新加载对sync()或async()的调用,而在另一个浏览器窗口中,我尝试加载index()页面。 Eventually (actually after eight calls to sync()/async()) all threads are blocked and index() does not load - until I unblock the thread by pressing enter on the terminal window. 最终(实际上是在对sync()/ async()进行了八次调用之后),所有线程都被阻塞,并且index()不会加载-直到我在终端窗口上按Enter取消阻塞线程为止。

This should explain all the things that you'd like to know: http://www.playframework.com/documentation/2.2.x/ThreadPools 这应该解释您想知道的所有事情: http : //www.playframework.com/documentation/2.2.x/ThreadPools

Shortly, it's not a thread-per-request model. 很快,它不是每个请求线程的模型。 Play uses Akka which is an implementation of a reactive programming principles. Play使用Akka,这是一种反应式编程原理的实现。 Oversimplified - there is a thread pool (or more of them) and once all threads of that pool are busy, then nothing else is executed. 过于简单化-有一个线程池(或更多线程池),并且一旦该池的所有线程都处于繁忙状态,则将不执行其他任何操作。 If you have a blocking code then there are ways how to deal with it explained in the article. 如果您有阻塞代码,那么本文中将介绍一些方法来处理它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM