Oct 29, 2018

Google App Engine Java Threads

A Java application can create a new thread, but there are some restrictions on how to do it. These threads can't "outlive" the request that creates them. (On a backend server, an application can spawn a background thread, a thread that can "outlive" the request that creates it.)

An application can


  • Implement java.lang.Runnable; and Create a thread factory by calling com.google.appengine.api.ThreadManager.currentRequestThreadFactory()
  • call the factory's newRequestThread method, passing in the Runnable, newRequestThread(runnable)
  • or use the factory object returned by com.google.appengine.api.ThreadManager.currentRequestThreadFactory() with an ExecutorService (e.g., call Executors.newCachedThreadPool(factory)).


However, you must use one of the methods on ThreadManager to create your threads. You cannot invoke new Thread() yourself or use the default thread factory.

An application can perform operations against the current thread, such as thread.interrupt().

Each request is limited to 50 concurrent request threads. The Java runtime will throw a java.lang.IllegalStateException if you try to create more than 50 threads in a single request.

Threads are a powerful feature that are full of surprises. If you aren't comfortable with using threads with Java, we recommend Brian Goetz, Java Concurrency in Practice.