-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Description
I'm using Java 8 and:
com.google.appengine:appengine-api-1.0-sdk:1.9.62
and this import:
import com.google.appengine.api.datastore.*;
I ran some code to test storing data into the Datastore. My testing is being done locally. This works fine if you perform the operation on the same thread that the servlet's doGet is on. I then created a runnable and ran the same code from within the runnable. I created the thread like this:
new Thread(myRunnable).start();
An exception was generated when the code in the runnable attempted to do this:
Entity someEntity = new Entity("myEntity", "someName");
The exception that got generated was:
No API environment is registered for this thread
This exception occurs in the ApiProxy.java class that is internal to the app engine sdk library.
I then replaced the line of code for creating the thread with:
ThreadManager.currentRequestThreadFactory().newThread(myRunnable)).start();
This worked fine. This tells me that you cannot use new Thread but need to use the newThread method. The documentation however indicates that using "new Thread" is allowed:
With the Java 8 runtime, you can create threads using App Engine's ThreadManager API and Java's built-in APIs, for example new Thread().
https://cloud.google.com/appengine/docs/standard/java/runtime-java8
This however is wrong. In the deprecated document in Java 7, it even states that you cannot use "new Thread", yet that statement was removed from the Java 8 doc. It should not have been. I recommend removing that statement as it is misleading and developers using it will end up with this exception. The deprecated statement should be reinserted to indicate that "new Thread" is not allowed when calling GAE APIs. new Thread does however work for normal Java stuff that isn't related to the GAE APIs.