Create a new Thread that executes runnable
independent of the current request. Calling this method is
equivalent to invoking ThreadFactory#newThread on the
ThreadFactory returned from #backgroundThreadFactory.
This method can currently only be used by backends.
Create a new Thread that executes runnable for
the duration of the current request. Calling this method is
equivalent to invoking ThreadFactory#newThread on the
ThreadFactory returned from #currentRequestThreadFactory.
This thread will be interrupted at the end of the current request
and must complete within the request deadline. If it fails to,
the instance containing it may be terminated.
Returns a ThreadFactory which will create threads scoped to the current request. These
threads will be interrupted at the end of the current request and must complete within the
request deadline. If they fail to, the instance containing them may be terminated.
The principal reason to use this method is so that the created threads can make App Engine
API calls (com.google.appengine.api.*). In general, threads not associated with a
request cannot make these API calls.
The returned factory is typically used with a call like java.util.concurrent.Executors#newCachedThreadPool(ThreadFactory). Do not use the ExecutorService returned by this call after the request
that created it has completed.
Note that calling ThreadFactory#newThread on the returned instance may throw any of
the unchecked exceptions mentioned by #createBackgroundThread.
Returns an Optional ThreadFactory which will create threads scoped to the current
request. These threads will be interrupted at the end of the current request and must complete
within the request deadline. If they fail to, the instance containing them may be terminated.
If this method is not called from an App Engine request thread, returns an empty Optional
instance.
The principal reason to use this method is so that the created threads can make App Engine
API calls (com.google.appengine.api.*). In general, threads not associated with a
request cannot make these API calls.
The returned factory is typically used with a call like java.util.concurrent.Executors#newCachedThreadPool(ThreadFactory). Do not use the ExecutorService returned by this call after the request
that created it has completed.
Note that calling ThreadFactory#newThread on the returned instance may throw any of
the unchecked exceptions mentioned by #createBackgroundThread.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-04 UTC."],[[["\u003cp\u003e\u003ccode\u003eThreadManager\u003c/code\u003e provides \u003ccode\u003eThreadFactory\u003c/code\u003e instances for App Engine applications to create and manage threads.\u003c/p\u003e\n"],["\u003cp\u003eIt offers methods to create threads that are independent of the current request, specifically for backends.\u003c/p\u003e\n"],["\u003cp\u003eThere are methods for creating threads scoped to the current request, allowing them to make App Engine API calls, and they will be interrupted upon request completion.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eThreadFactory\u003c/code\u003e returned by \u003ccode\u003ebackgroundThreadFactory\u003c/code\u003e is specifically intended for creating background threads, while \u003ccode\u003ecurrentRequestThreadFactory\u003c/code\u003e is intended for threads tied to the duration of a specific request.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003ecurrentRequestThreadFactoryOptional\u003c/code\u003e provides an \u003ccode\u003eOptional\u003c/code\u003e thread factory that is empty if the method is not called from an App Engine request thread, allowing for conditional thread creation.\u003c/p\u003e\n"]]],[],null,["# Class ThreadManager (2.0.0)\n\n public final class ThreadManager\n\n`ThreadManager` exposes a [ThreadFactory](https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ThreadFactory.html) that allows\nApp Engine applications to spawn new threads.\n\nRefer to [this discussion of threads](https://cloud.google.com/appengine/docs/java/runtime#threads) for drawbacks of thread usage and possible\nalternatives. \n\nInheritance\n-----------\n\n[java.lang.Object](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html) \\\u003e ThreadManager \n\nInherited Members\n-----------------\n\n[Object.clone()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#clone--) \n[Object.equals(Object)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#equals-java.lang.Object-) \n[Object.finalize()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#finalize--) \n[Object.getClass()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#getClass--) \n[Object.hashCode()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#hashCode--) \n[Object.notify()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#notify--) \n[Object.notifyAll()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#notifyAll--) \n[Object.toString()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#toString--) \n[Object.wait()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait--) \n[Object.wait(long)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait-long-) \n[Object.wait(long,int)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait-long-int-)\n\nStatic Methods\n--------------\n\n### backgroundThreadFactory()\n\n public static ThreadFactory backgroundThreadFactory()\n\nReturns a [ThreadFactory](https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ThreadFactory.html) that will create threads that are\nindependent of the current request.\n\nThis ThreadFactory can currently only be used by backends.\n\nNote that calling ThreadFactory#newThread on the\nreturned instance may throw any of the unchecked exceptions\nmentioned by [#createBackgroundThread](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.ThreadManager#com_google_appengine_api_ThreadManager_createBackgroundThread_).\n\n### createBackgroundThread(Runnable runnable)\n\n public static Thread createBackgroundThread(Runnable runnable)\n\nCreate a new [Thread](https://docs.oracle.com/javase/8/docs/api/java/lang/Thread.html) that executes `runnable`\nindependent of the current request. Calling this method is\nequivalent to invoking ThreadFactory#newThread on the\nThreadFactory returned from [#backgroundThreadFactory](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.ThreadManager#com_google_appengine_api_ThreadManager_backgroundThreadFactory_).\n\nThis method can currently only be used by backends.\n\n### createThreadForCurrentRequest(Runnable runnable)\n\n public static Thread createThreadForCurrentRequest(Runnable runnable)\n\nCreate a new [Thread](https://docs.oracle.com/javase/8/docs/api/java/lang/Thread.html) that executes `runnable` for\nthe duration of the current request. Calling this method is\nequivalent to invoking ThreadFactory#newThread on the\nThreadFactory returned from [#currentRequestThreadFactory](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.ThreadManager#com_google_appengine_api_ThreadManager_currentRequestThreadFactory_).\nThis thread will be interrupted at the end of the current request\nand must complete within the request deadline. If it fails to,\nthe instance containing it may be terminated.\n\n### currentRequestThreadFactory()\n\n public static ThreadFactory currentRequestThreadFactory()\n\nReturns a [ThreadFactory](https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ThreadFactory.html) which will create threads scoped to the current request. These\nthreads will be interrupted at the end of the current request and must complete within the\nrequest deadline. If they fail to, the instance containing them may be terminated.\n\nThe principal reason to use this method is so that the created threads can make App Engine\nAPI calls (`com.google.appengine.api.*`). In general, threads not associated with a\nrequest cannot make these API calls.\n\nThe returned factory is typically used with a call like java.util.concurrent.Executors#newCachedThreadPool(ThreadFactory). Do not use the ExecutorService returned by this call after the request\nthat created it has completed.\n\nNote that calling ThreadFactory#newThread on the returned instance may throw any of\nthe unchecked exceptions mentioned by [#createBackgroundThread](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.ThreadManager#com_google_appengine_api_ThreadManager_createBackgroundThread_).\n\n### currentRequestThreadFactoryOptional()\n\n public static Optional\u003cThreadFactory\u003e currentRequestThreadFactoryOptional()\n\nReturns an Optional [ThreadFactory](https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ThreadFactory.html) which will create threads scoped to the current\nrequest. These threads will be interrupted at the end of the current request and must complete\nwithin the request deadline. If they fail to, the instance containing them may be terminated.\n\nIf this method is not called from an App Engine request thread, returns an empty Optional\ninstance.\n\nThe principal reason to use this method is so that the created threads can make App Engine\nAPI calls (`com.google.appengine.api.*`). In general, threads not associated with a\nrequest cannot make these API calls.\n\nThe returned factory is typically used with a call like java.util.concurrent.Executors#newCachedThreadPool(ThreadFactory). Do not use the ExecutorService returned by this call after the request\nthat created it has completed.\n\nNote that calling ThreadFactory#newThread on the returned instance may throw any of\nthe unchecked exceptions mentioned by [#createBackgroundThread](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.ThreadManager#com_google_appengine_api_ThreadManager_createBackgroundThread_).\n\nConstructors\n------------\n\n### ThreadManager()\n\n public ThreadManager()"]]