Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Examples: Updated HelloWorldServer to use Executor #11850

Merged
merged 12 commits into from
Feb 18, 2025
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
* Server that manages startup/shutdown of a {@code Greeter} server.
Expand All @@ -31,11 +33,20 @@ public class HelloWorldServer {
private static final Logger logger = Logger.getLogger(HelloWorldServer.class.getName());

private Server server;

private void start() throws IOException {
/* The port on which the server should run */
int port = 50051;
/*
* By default gRPC uses a global, shared Executor.newCachedThreadPool() for gRPC callbacks into
* your application. This is convenient, but can cause an excessive number of threads to be
* created if there are many RPCs. It is often better to limit the number of threads your
* application uses for processing and let RPCs queue when the CPU is saturated.
* The appropriate number of threads varies heavily between applications.
* Async application code generally does not need more threads than CPU cores.
*/
ExecutorService executor = Executors.newFixedThreadPool(2);
server = Grpc.newServerBuilderForPort(port, InsecureServerCredentials.create())
.executor(executor)
.addService(new GreeterImpl())
.build()
.start();
Expand All @@ -48,7 +59,12 @@ public void run() {
try {
HelloWorldServer.this.stop();
} catch (InterruptedException e) {
if (server != null) {
server.shutdownNow();
}
e.printStackTrace(System.err);
} finally {
executor.shutdown();
}
System.err.println("*** server shut down");
}
Expand Down