Play 框架控制器动作是否同步? [英] Are Play framework controller actions synchronized?

查看:62
本文介绍了Play 框架控制器动作是否同步?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近正在测试一些代码,当我注意到每个控制器操作/方法一次只有一个线程处于活动状态时,我有点惊讶.例如,考虑控制器的以下 2 个操作:

I was testing some code recently and was a little surprised when I noticed only one thread was active at a time per controller action/method. For example, consider the following 2 actions for a controller:

public static void testThread()
{
    Logger.info("Start");

    try
    {
        Logger.info("Sleeping...");
        Thread.sleep(30000);
    }
    catch (Exception e)
    {

    }

    Logger.info("End");
}

public static void testThread2()
{
    Logger.info("Start");

    try
    {
        Logger.info("Sleeping...");
        Thread.sleep(30000);
    }
    catch (Exception e)
    {

    }

    Logger.info("End");
}

从 2 个浏览器会话调用 testThread(),在任何给定时间只执行 1 个.第二个请求一直等到第一个请求完成.但是,从一个浏览器调用 testThread() 并从另一个浏览器调用 testThread2() 会产生预期的结果.只是为了澄清默认线程 (play.pool) 的数量增加了,并且在 Tomcat 中作为 WAR 运行时也存在此行为.

Calling testThread() from 2 browser sessions, only 1 is executed at any given time. The second request waits until the first is finished. However, calling testThread() from one browser and testThread2() from another yields the expected results. Just to clarify that the number of default threads (play.pool) was increased and this behavior also exists when running as a WAR in Tomcat.

谁能解释为什么会发生这种情况,以及是否有办法防止这种行为?

Can anybody explain why this is happening and if there is a way to prevent this behavior?

推荐答案

经过多次测试,是同一个浏览器还是不同的浏览器(可能取决于播放会话)会有所不同.

After doing more testing, it makes a difference if it's the same browser or a different browser (probably depending on the play session).

例如,同时访问同一个控制器的 2 个 Chrome 标签将是连续的.

For instance, 2 Chrome tabs accessing the same controller at the same time will be sequential.

但是,Chrome 中的 1 个和 Safari 中的 1 个将按预期进行多线程.

However, 1 from Chrome and 1 from Safari will be multi-threaded as expected.

这篇关于Play 框架控制器动作是否同步?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆