Class ThreadManager
- java.lang.Object
-
- com.google.appengine.api.ThreadManager
-
public final class ThreadManager extends java.lang.Object
ThreadManager
exposes aThreadFactory
that allows App Engine applications to spawn new threads. Refer to this discussion of threads for drawbacks of thread usage and possible alternatives.
-
-
Constructor Summary
Constructors Constructor and Description ThreadManager()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method and Description static java.util.concurrent.ThreadFactory
backgroundThreadFactory()
Returns aThreadFactory
that will create threads that are independent of the current request.static java.lang.Thread
createBackgroundThread(java.lang.Runnable runnable)
Create a newThread
that executesrunnable
independent of the current request.static java.lang.Thread
createThreadForCurrentRequest(java.lang.Runnable runnable)
Create a newThread
that executesrunnable
for the duration of the current request.static java.util.concurrent.ThreadFactory
currentRequestThreadFactory()
Returns aThreadFactory
that will create threads scoped to the current request.
-
-
-
Method Detail
-
currentRequestThreadFactory
public static java.util.concurrent.ThreadFactory currentRequestThreadFactory()
Returns aThreadFactory
that 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.Your code has limited access to the threads created by this
ThreadFactory
. For example, you can callThread.setUncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler)
andThread.interrupt()
, but notThread.stop()
or any other methods that requireRuntimePermission("modifyThread")
.The Java runtime will throw java.lang.IllegalStateException if you try to create more than 50 threads in a single request.
Note that calling
ThreadFactory.newThread(java.lang.Runnable)
on the returned instance may throw any of the unchecked exceptions mentioned bycreateBackgroundThread(java.lang.Runnable)
.- Throws:
java.lang.NullPointerException
- if the calling thread is not associated with a request.
-
createThreadForCurrentRequest
public static java.lang.Thread createThreadForCurrentRequest(java.lang.Runnable runnable)
Create a newThread
that executesrunnable
for the duration of the current request. Calling this method is equivalent to invokingThreadFactory.newThread(java.lang.Runnable)
on the ThreadFactory returned fromcurrentRequestThreadFactory()
. This thread will be interrupted at the end of the current request and must complete within the request deadline.- Throws:
java.lang.IllegalStateException
- if you try to create more than 50 threads in a single request.java.lang.NullPointerException
- if the calling thread is not associated with a request.ApiProxy.FeatureNotEnabledException
- If this application cannot use this feature.
-
backgroundThreadFactory
public static java.util.concurrent.ThreadFactory backgroundThreadFactory()
Returns aThreadFactory
that will create threads that are independent of the current request.This ThreadFactory can currently only be used by backends.
Note that calling
ThreadFactory.newThread(java.lang.Runnable)
on the returned instance may throw any of the unchecked exceptions mentioned bycreateBackgroundThread(java.lang.Runnable)
.
-
createBackgroundThread
public static java.lang.Thread createBackgroundThread(java.lang.Runnable runnable)
Create a newThread
that executesrunnable
independent of the current request. Calling this method is equivalent to invokingThreadFactory.newThread(java.lang.Runnable)
on the ThreadFactory returned frombackgroundThreadFactory()
.This method can currently only be used by backends.
- Throws:
ApiProxy.FeatureNotEnabledException
- If this application cannot use this feature.ApiProxy.CancelledException
- If the request was interrupted while creating the new thread.ApiProxy.ApiDeadlineExceededException
- If creation of the new thread took too long.
-
-