Search code examples
javamultithreadinggoogle-app-enginetask-queue

Creating Threads with java in AppEngine Standard Environment


I'm new in Google Cloud Platform. I'm using AppEngine standard Environment. I need to create Threads in java but I think it's not possible, is it?

Here is the situation:

I need to create Feeds for users.

There are three databases with names d1, d2, d3.

Whenever a user sends a request for feeds Java creates three threads, one for each database. For example t1 for d1, t2 for d2 and t3 for d3. These threads must run asynchronously for better performance and after that the data from these 3 threads is combined and sent in the response back to user.

I know how to write code for this, but as you know I need threads for this work. If AppEngine standard Env. doesn't allow it then what can I do? Is there any other way?

In GCP Documentation they said:

To avoid using threads, consider Task Queues

I read about Task Queues. There are two types of queues: Push and Pull. Both run asynchronously but they do not send a response back to the user. I think they are only designed to complete tasks in the background.

Can you please let me know how can I achieve my goal? What things I need to learn for this?


Solution

  • Note: the answer is based solely on documentation, I'm not a java user.

    Threads are supported by the standard environment, but with restrictions. From Threads:

    Caution: Threads are a powerful feature that are full of surprises. To learn more about using threads with Java, we recommend Goetz, Java Concurrency in Practice.

    A Java application can create a new thread, but there are some restrictions on how to do it. These threads can't "outlive" the request that creates them.

    An application can

    • Implement java.lang.Runnable.
    • Create a thread factory by calling com.google.appengine.api.ThreadManager.currentRequestThreadFactory().
    • Call the factory's newRequestThread method, passing in the Runnable, newRequestThread(runnable), or use the factory object returned by com.google.appengine.api.ThreadManager.currentRequestThreadFactory() with an ExecutorService (e.g., call Executors.newCachedThreadPool(factory)).

    However, you must use one of the methods on ThreadManager to create your threads. You cannot invoke new Thread() yourself or use the default thread factory.

    An application can perform operations against the current thread, such as thread.interrupt().

    Each request is limited to 50 concurrent request threads. The Java runtime will throw a java.lang.IllegalStateException if you try to create more than 50 threads in a single request.

    When using threads, use high level concurrency objects, such as Executor and Runnable. Those take care of many of the subtle but important details of concurrency like Interrupts and scheduling and bookkeeping.