Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
This blog will study how to configure your application's Play's Thread pools. You will be given insights into what thread pools are in play and why they are used in your application.
You will further see different methods to configure the framework in your application. You will further understand how these thread pools are essential in your web- application.
Play and Thread
Play framework is an asynchronous framework. Thread pools in play are used to increase your application's performance when you run many tasks in your application in a single go.
If you are doing a lot of extensive CPU usage, you must know which thread will be used to operate.
Play has shown that when appropriately configured, it can manage and run tons of work without any problem and improve the application's working.
Play Execution Context
Execution Context is another name for Thread in PlayFramework. Play uses several thread pools for different purposes.
Internal Thread pools: The server engine uses these internally for processing IO. Never permit a thread in one of these thread pools to run code from an application. Play is configured in either of the two ways.
Akka HTTP server backend: The Akka HTTP server backend is configured by default. Application.conf configuration settings must be used to modify the backend settings.
Netty server backend: The Netty Server backend can be applied explicitly, whose configuration settings can also be configured from the application.conf.
Default Thread Pool
Every function in Play Framework uses the default thread pool. In most situations, the thread pool uses the default thread pool method. Through @Inject(), you can access this (implicit ec: ExecutionContext)
import play.libs.concurrent.HttpExecutionContext;
import play.mvc.*;
import javax.inject.Inject;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
public class MyController extends Controller {
private HttpExecutionContext httpExecutionContext;
@Inject
public MyController(HttpExecutionContext ec) {
this.httpExecutionContext = ec;
}
public CompletionStage<Result> index() {
// Use a different task with explicit EC
return calculateResponse()
.thenApplyAsync(
answer -> {
return ok("answer was " + answer).flashing("info", "Response updated!");
},
httpExecutionContext.current());
}
private static CompletionStage<String> calculateResponse() {
return CompletableFuture.completedFuture("42");
}
}
This execution context uses the default dispatcher provided by Akka and connects directly to the application's actor system.
Configuring the Default & Using other Thread Pools
Under the Akka namespace in the application.conf, the default thread pool can be set up using the usual Akka configuration.
In some cases, you might want to distribute work to various thread pools in some situations. This could involve IO work, such as database access or CPU-intensive tasks. You must first establish a ThreadPool, which is simple to do in Scala:
val myExecutionContext: ExecutionContext = akkaSystem.dispatchers.lookup("my-context")
To configure this Akka executionContext, enable this configuration in the application.conf
Inscala, use the scalaFuturecompanion object function.
Future {
// Some blocking or expensive code here
}(myExecutionContext)
Or include it implicitly.
implicit val ec = myExecutionContext
Future {
// Some blocking or expensive code here
}
Class Loaders
Class loaders require more attention in a multithreaded environment.
It's possible that the thread context class loader in a Play application won't always be able to load application classes. To load classes, you need to use the application class loader specifically.
Java
Class myClass = app.classloader().loadClass(myClassName);
Scala
val myClass = app.classloader.loadClass(myClassName)
Using this explicitly to load classes is a must when Play is in development mode and not in production. This is because play in development mode uses multiple class loaders for automatic application reloading.
When using third-party libraries, it might happen that sometimes you are unable to load class explicitly. At that time, it is guided that before you call the third party, you explicitly set the thread context class loader. Once you're done calling the outside code, restore the context class loader to its default setting.
Frequently Asked Questions
What is Play Framework?
It is an open-source web application framework that follows the MVC model structure. MVC stands for Model, View, and Controller.
What does Play Thread Pools mean?
Play is Configured with Akka HTTP server by default, so configuration settings from the application.conf. Play also has a second configuration known as the Netty Server backend.
What happens when the thread pool is full?
By default, the Max threads of the Thread pool are very high. So generally, your app will crash first, and even if the thread pool is complete, the new tasks are queued, and slowly Thread pools allocate the new threads.
What are the advantages of thread pooling?
Thread pools are easy to access. It helps to improve performance, and there is no need to create new threads repeatedly.
How many types of thread pools are present in java?
We have five types of thread pool executors with the pre-built methods in java. util.concurrent package.
Conclusion
In this blog, we have discussed configuring Play Thread pools. We have further seen how this thread helps increase the application's performance. We have seen the configuration of Thread in Java and Scala. We have understood how class loaders work and their usage.