GAE线程池不一致

时间:2017-10-10 22:27:45

标签: multithreading google-app-engine threadpool java-7

我正在Google App Engine上设置服务。我需要该服务进行五次外部服务调用,合并他们的结果,并返回合并的答案。外部服务不受我的控制。

我设置了一个这样的线程池:

import com.google.appengine.api.ThreadManager;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;

// ...

Executor executor = new ThreadPoolExecutor(3,
    10,
    30,
    TimeUnit.SECONDS,
    new LinkedBlockingQueue<>(50),
    ThreadManager.currentRequestThreadFactory());
    // I looked into the backgroundThreadFactory(), but that would only
    // work with a backend service, which is deprecated.

// ...

Collection<Result> doWork(List<Task> tasks) {
  final ResultCollector collector = new ResultCollector(); // My own class to join results.
  final CountDownLatch latch = new CountDownLatch(tasks.size());
  for (Task task : tasks) { // My own worker class
    executor.execute(new Runnable() {
      @Override
      public void run() {
        task.execute(collector); // Does work, populates collector.
        latch.countDown();
      }
    });
  }

  latch.await();
  return collector.getResult();
}

然后我的服务电话不时开始超时。我进行了一些日志记录,发现在某些情况下Runnable run()来电未被调用十秒或更长时间。我不确定这是否是因为App Engine正在为每个请求创建一个新的线程池。正如我在评论中所说,我不能使用backgroundThreadFactory,因为它不是后端实例。

是否有更高效的方法在App Engine中并行进行这些类型的调用?

0 个答案:

没有答案