Skip to content

new Thread is not supported when calling GAE APIs #2960

Description

@DrifterAtSea

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.

Activity

  1. 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
  2. added
    type: questionRequest for information or clarification. Not an issue.
    api: datastoreIssues related to the Datastore API.
    priority: p2Moderately-important priority. Fix may not be included in next release.
    on Feb 27, 2018
  3. pongad commented on Feb 28, 2018

    @pongad
    Contributor

    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> in appengine-web.xml as 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.

  4. self-assigned this
    on Feb 28, 2018
  5. DrifterAtSea commented on Feb 28, 2018

    @DrifterAtSea
    Author

    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.

  6. DrifterAtSea commented on Feb 28, 2018

    @DrifterAtSea
    Author

    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.

  7. pongad commented on Feb 28, 2018

    @pongad
    Contributor

    @JohannBlake You're right. The APIs are documented here, but I can't seem to find the official repo now. @wsh can you help?

  8. wsh commented on Feb 28, 2018

    @wsh

    @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.

  9. pongad commented on Mar 6, 2018

    @pongad
    Contributor

    Since the previous response seems to answer the question, I'll close this for now. Please reopen if you have other.

  10. added a commit that references this issue on Feb 20, 2026
  11. added a commit that references this issue on Mar 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api: datastoreIssues related to the Datastore API.priority: p2Moderately-important priority. Fix may not be included in next release.type: questionRequest for information or clarification. Not an issue.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions