等待守护程序线程使用执行程序服务

时间:2016-09-12 05:40:13

标签: java multithreading synchronized executorservice atomicinteger

我必须并行化现有的后台任务,而不是连续消费&#39; x&#39;资源,平行完成手头的工作只使用&#39; y&#39;线程(y <&lt; x)。此任务不断在后台运行,并继续处理一些资源。

代码结构如下:

class BaseBackground implements Runnable {
    @Override
    public void run() {
        int[] resources = findResources(...);

        for (int resource : resources) {
            processResource(resource);
        }

        stopProcessing();
     }

    public abstract void processResource(final int resource);
    public void void stopProcessing() {
         // Override by subclass as needed
    }
}

class ChildBackground extends BaseBackground {

    @Override
    public abstract void processResource(final int resource) {
        // does some work here
    }

    public void void stopProcessing() {
        // reset some counts and emit metrics
    }
}

我已按以下方式修改ChildBackground

class ChildBackground extends BaseBackground {

    private final BlockingQueue<Integer> resourcesToBeProcessed;

    public ChildBackground() {
        ExecutorService executorService = Executors.newFixedThreadPool(2);
        for (int i = 0; i < 2; ++i) {
             executorService.submit(new ResourceProcessor());
        }
    }

    @Override
    public abstract void processResource(final int resource) {
        resourcesToBeProcessed.add(resource);
    }

    public void void stopProcessing() {
        // reset some counts and emit metrics
    }

    public class ResourceProcessor implements Runnable {
        @Override
        public void run() {
            while (true) {
                int nextResource = resourcesToBeProcessed.take();
                // does some work
            }
        }
    }
}

我不是每次都创建和删除ExecutorService,因为垃圾收集在我的服务中有点问题。虽然,我不明白它有多糟糕,因为我不会在每次迭代中产生超过10个线程。

我无法理解如何等待所有ResourceProcessor s完成一次迭代的处理资源,这样我就可以重置一些计数并在stopProcessing中发出指标。我考虑过以下几种选择:

1)executorService.awaitTermination(timeout)。这不会真正起作用,因为它会一直阻塞直到超时,因为ResourceProcessor线程永远不会真正完成他们的工作

2)我可以找出findResources之后的资源数量,并将其提供给子类,并使每个ResourceProcessor增加处理的资源数量。在重置计数之前,我必须等待stopProcessing中处理所有资源。我需要像CountDownLatch这样的东西,但它应该算作UP。在这个选项中会有很多国家管理,我不是特别喜欢。

3)我可以更新public abstract void processResource(final int resource)以包括总资源数,并让子进程等待,直到所有线程都处理了总资源。在这种情况下,也会有一些州管理,但它只限于子类。

在两种情况中的任何一种情况下,我都要添加wait()&amp; notify()逻辑,但我对我的方法没有信心。这就是我所说的:

class ChildBackground extends BaseBackground {

    private static final int UNSET_TOTAL_RESOURCES = -1;

    private final BlockingQueue<Integer> resourcesToBeProcessed;

    private int totalResources = UNSET_TOTAL_RESOURCES;
    private final AtomicInteger resourcesProcessed = new AtomicInteger(0);

    public ChildBackground() {
        ExecutorService executorService = Executors.newFixedThreadPool(2);
        for (int i = 0; i < 2; ++i) {
             executorService.submit(new ResourceProcessor());
        }
    }

    @Override
    public abstract void processResource(final int resource, final int totalResources) {
        if (this.totalResources == UNSET_TOTAL_RESOURCES) {
            this.totalResources = totalResources;
        } else {
            Preconditions.checkState(this.totalResources == totalResources, "Consecutive poll requests are using different total resources count, previous=%s, new=%s", this.totalResources, totalResources);
        }
        resourcesToBeProcessed.add(resource);
    }

    public void void stopProcessing() {
        try {
            waitForAllResourcesToBeProcessed();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        resourcesProcessed.set(0);
        totalResources = UNSET_TOTAL_RESOURCES;
        // reset some counts and emit metrics
    }

    private void incrementProcessedResources() {
        synchronized (resourcesProcessed) {
            resourcesProcessed.getAndIncrement();
            resourcesProcessed.notify();
        }
    }

    private void waitForAllResourcesToBeProcessed() throws InterruptedException {
        synchronized (resourcesProcessed) {
             while (resourcesProcessed.get() != totalResources) {
                resourcesProcessed.wait();
             }
        }
    }

    public class ResourceProcessor implements Runnable {
        @Override
        public void run() {
            while (true) {
                int nextResource = resourcesToBeProcessed.take();
                try {
                   // does some work
                } finally {
                   incrementProcessedResources();
                }
            }
        }
    }
}

我不确定使用AtomicInteger是否是正确的方法,如果是这样,我是否需要调用wait()和notify()。如果我没有使用wait()和notify(),我甚至不必在同步块中执行所有操作。

请告诉我您对此方法的看法,如果我只是为每次迭代创建并关闭ExecutorService,或者我应该采用第四种方法。

1 个答案:

答案 0 :(得分:1)

您的代码似乎不必要地复杂。当ExecutorService内有队列时,为什么要拥有自己的队列?当我认为你可以让股票ExecutorService为你处理它时,你必须做一大堆管理。

我将你的工作定义为:

public static class ResourceProcessor implements Runnable {
   private final int resource;
   public ResourceProcessor(int resource) {
      this.resource = resource;
   }
   public void run() {
      try {
         // does some work
      } finally {
         // if this is still necessary then you should use a `Future` instead
         incrementProcessedResources();
      }
   }
}

然后你可以提交它们:

ExecutorService executorService = Executors.newFixedThreadPool(2);
for (int i = 0; i < totalResources; ++i) {
     executorService.submit(new ResourceProcessor(i));
}
// shutdown the thread pool after the last submit
executorService.shutdown();
  

executorService.awaitTermination(timeout)。这不会真正起作用,因为它总是阻塞直到超时,因为ResourceProcessor线程永远不会真正完成他们的工作

现在可以了。

  

2)我可以找出已经完成的资源数量。

如果你可以致电awaitTermination(...),还需要这个吗?

  

3)我可以更新public abstract void processResource(final int resource)以包含总资源的数量,让子进程等到所有线程都处理完总资源...

同样的问题。这需要吗?

如果您确实需要知道已处理的请求列表,那么您可以像@ScaryWombat一样提到使用Future<Integer>Callable<Integer>或使用ExecutorCompletionService

  

期货不是一种选择,因为执行程序线程在紧密循环内运行,只有当服务被停用时才会停止。

你能解释一下吗?

希望这有帮助。