问题
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");
}
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?
回答1:
After doing more testing, it makes a difference if it's the same browser or a different browser (probably depending on the play session).
For instance, 2 Chrome tabs accessing the same controller at the same time will be sequential.
However, 1 from Chrome and 1 from Safari will be multi-threaded as expected.
回答2:
Play actions are static, not synchronous.
What is synchronous however is the Play Handler threads. In Dev mode, it defaults to a single thread, so therefore, this is likely what you are seeing.
回答3:
I retested and it turns out this is a browser feature(when in playframework production mode). I sniffed with wireshark and the browser did NOT send the second request until the first request had finished. This had nothign to do with the playframework it turns out, but interesting find.
sooooo, when playframework is in production mode, it is not doing any synchronization even if it is the same client however the client's have their own behavior and may do synchronization before sending the second request.
来源:https://stackoverflow.com/questions/9331646/are-play-framework-controller-actions-synchronized