Repository navigation
new Thread is not supported when calling GAE APIs #2960
Description
Activity
- changed the title
[-]new Thread is not supported in Java 8 GAE[/-][+]new Thread is not supported when calling GAE APIs[/+]on Feb 27, 2018 - addedtype: questionRequest for information or clarification. Not an issue.Request for information or clarification. Not an issue.api: datastoreIssues related to the Datastore API.Issues related to the Datastore API.priority: p2Moderately-important priority. Fix may not be included in next release.Moderately-important priority. Fix may not be included in next release.
on Feb 27, 2018 CC @wsh
Please correct me if I'm wrong. I think
com.google.appengine.api.datastore.*is not in this repo.EDIT: wrong repo earlier.
But to answer your question, I suspect that you're using Java 8 but your local AppEngine server is still using Java 7 runtime. I believe you need a line
<runtime>java8</runtime>inappengine-web.xmlas described here.I just tested this with
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { new Thread(new Runnable(){ @Override public void run() { System.out.println("running from thread!"); } }).run(); }
and the code properly printed the message to my console.
I indicated that the issue has to do with accessing the GAE APIs from within a thread using "new Thread". The example you posted above does not do that - that's just running a normal thread without accessing the GAE APIs, which I indicated works fine. Also, my appengine-web.xml does contain the java8 runtime flag. Thanks all the same.
Actually, that link you provided shows a repo that is 7 years old and hasn't been touched since. Something doesn't seem right. It's not even an official Google repo.
@JohannBlake You're right. The APIs are documented here, but I can't seem to find the official repo now. @wsh can you help?
@JohannBlake the key part is in fact the sentence immediately after the one you quote:
Currently, if you want to call App Engine APIs (com.google.appengine.api.*), you must call those APIs from a request thread or from a thread created using the ThreadManager API.
We don't have a canonical repository for the App Engine Java SDK that I'm aware of.
Since the previous response seems to answer the question, I'll close this for now. Please reopen if you have other.
- added a commit that references this issue
on Feb 20, 2026 - added a commit that references this issue
on Mar 20, 2026 - added 4 commits that reference this issue
on Mar 24, 2026
I'm using Java 8 and:
com.google.appengine:appengine-api-1.0-sdk:1.9.62and 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:
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:
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.