我试图在这种情况下(任何版本)找出Java的最佳方法来实现单个生产者多个使用者,我使用ExecutorService(优先,不需要)生产者需要运行&# 34;永远"但是每次运行时,它都需要等待所有内容完全处理,例如所有消费者线程都已终止,队列为空,并且不会生成任何项目。生产者也应该只以固定的间隔轮询其数据源。
举个例子:每30分钟,我希望我的生产者轮询其数据源以获取记录并将其提交到队列中。如果消费者需要超过30分钟的处理时间,我希望生产者等到所有项目都被处理后再次轮询其数据源(从30分钟开始就会立即这样做)。
不寻找有人为我编写代码。一些基本的提示/指导将非常感激。
以下是我尝试使用的简短示例实现。我已经完成了解决问题的所有可怕尝试。请注意,构造ThreadPoolExecutor的硬编码参数最终将是动态的。
import java.util.concurrent.*;
public class ItemProcessorService {
public static void main(String args[]) throws InterruptedException {
RejectedExecutionHandlerImpl rejectionHandler = new RejectedExecutionHandlerImpl();
ThreadFactory threadFactory = Executors.defaultThreadFactory();
int corePoolSize = 5,
maxPoolSize = 10,
keepAlive = 10,
queueCapacity = 1;
ThreadPoolExecutor executor = new ThreadPoolExecutor(corePoolSize, maxPoolSize, keepAlive, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(queueCapacity), threadFactory, rejectionHandler);
for (int i = 0; i < 10; i++) {
executor.execute(new ItemConsumer());
}
executor.shutdown();
while (!executor.isTerminated()) {
}
System.out.println("Executor finished");
}
}
class ItemConsumer implements Runnable {
@Override
public void run() {
processItem();
}
private void processItem() {
try {
Thread.sleep(3000);
System.out.println(Thread.currentThread().getName() + " - processed item");
} catch (InterruptedException e) {
//e.printStackTrace();
}
}
}
class RejectedExecutionHandlerImpl implements RejectedExecutionHandler {
@Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
System.out.println(r.toString() + " - rejected");
}
}
答案 0 :(得分:1)
寻找具有未来任务的线程池执行器。它将使您的poducer在所有线程完成之前等待。以下是示例:
for (int job = 0; job < yourList.size(); job++) {
Future<String[]> future = Service.getCompletionService().take();
if (future.isDone()) {
}
}
答案 1 :(得分:0)
使用自调度生成器(另请参阅here)和CountDownLatch。我知道你不需要代码,但我认为在这种情况下,有一些代码可以用来试验:
import java.util.*;
import java.util.concurrent.*;
public class WaitForConsumers {
public static void main(String[] args) {
try {
new WaitForConsumers().demo();
} catch (Exception e) {
e.printStackTrace();
}
}
public static final int NUMBER_OF_SLEEP_TASKS = 100;
public void demo() throws Exception {
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
Producer p = new Producer(scheduler, 1000L);
p.start();
try {
Thread.sleep(3000);
p.stop();
System.out.println("Stopped.");
} finally {
scheduler.shutdownNow();
}
}
public static List<Long> produce() {
List<Long> items = new ArrayList<Long>();
for (int i = 0; i < NUMBER_OF_SLEEP_TASKS; i++) items.add(i * 1L);
return items;
}
public static void consume(List<Runnable> tasks, CountDownLatch consumersRunning) throws Exception {
ExecutorService executor = Executors.newCachedThreadPool();
for (Runnable r : tasks) executor.execute(r);
try {
consumersRunning.await();
} finally {
executor.shutdownNow();
}
}
class Producer implements Runnable {
ScheduledExecutorService scheduler;
long frequencyMs;
volatile boolean stop;
ScheduledFuture<?> producerTask;
Producer(ScheduledExecutorService scheduler, long frequencyMs) {
this.scheduler = scheduler;
this.frequencyMs = frequencyMs;
}
void start() {
scheduler.execute(this);
}
void stop() {
System.out.println("Stopping producer.");
stop = true;
if (producerTask != null) {
producerTask.cancel(true);
}
}
@Override public void run() {
long startTime = System.currentTimeMillis();
List<Long> items = produce();
CountDownLatch consumersRunning = new CountDownLatch(items.size());
List<Runnable> tasks = wrap(items, consumersRunning);
try {
System.out.println("Consuming " + tasks.size() + " tasks.");
consume(tasks, consumersRunning);
System.out.println("Consumed tasks.");
} catch (Exception e) {
e.printStackTrace();
stop = true;
} finally {
if (stop) {
System.out.println("Producer stopping.");
} else {
long waitTime = frequencyMs - (System.currentTimeMillis() - startTime);
if (waitTime < 1L) {
scheduler.execute(this);
} else {
System.out.println("Next run in " + waitTime + " ms.");
producerTask = scheduler.schedule(this, waitTime, TimeUnit.MILLISECONDS);
}
}
}
}
List<Runnable> wrap(List<Long> items, final CountDownLatch consumersRunning) {
List<Runnable> tasks = new ArrayList<Runnable>();
for (Long l : items) {
tasks.add(new SleepTask(l, consumersRunning));
}
return tasks;
}
} // class Producer
class SleepTask implements Runnable {
long sleepTime;
CountDownLatch consumersRunning;
public SleepTask(long sleepTime, CountDownLatch consumersRunning) {
this.sleepTime = sleepTime;
this.consumersRunning = consumersRunning;
}
@Override public void run() {
try {
Thread.sleep(sleepTime);
} catch (Exception e) {
e.printStackTrace();
} finally {
consumersRunning.countDown();
}
}
} // class SleepTask
}
答案 2 :(得分:0)
您可以通过为生产者的每次运行创建ExecutorService
来很好地解决此问题。制作人创建它,关闭它,并等待它的终止。
要安排制作人,请使用ScheduledExecutorService.scheduleWithFixedDelay(...)
或ScheduledExecutorService.scheduleAtFixedRate(...)
,具体取决于您的需要:
scheduleWithFixedDelay(...)
将在两次运行之间保持指定的时间量,因此无论运行持续多长时间,下一次运行将在指定的时间后继续:
...|XXXXXX|.............|XXXXX|.............|XXXXXXXXX|.............|X
<--- fix ---> <--- fix ---> <--- fix --->
scheduleAtFixedRate(...)
尝试保持调度速率,因此如果生产者需要更长时间,两次运行之间的时间将减少,但两次运行将永远不会重叠:
...|XXXXXXXX|...|XXXXX|......|XXXXXXXXXXXXXXX||XXX|....|XX
<--- fix ---><--- fix ---><--- fix ---><--- fix ---><-
生产者看起来如何的简单例子:
public class Producer implements Runnable {
public void run() {
ExecutorService executor = ... // create new executor
// queue items
for (Object item : itemSource) {
executor.schedule(new Consumer(item));
}
// shutdown executor
executor.shutdown();
executor.awaitTermination(2, TimeUnit.HOURS);
}
}
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
scheduler.scheduleWithFixedDelay(new Producer(), 30, 30, TimeUnit.MINUTES);
// or
scheduler.scheduleAtFixedRate(new Producer(), 30, 30, TimeUnit.MINUTES);
当然,您必须进行适当的异常处理,因为未捕获的生产者运行中的每个异常都会阻止调度程序重新安排它。
请注意,创建执行程序可能会很昂贵,所以这种方法是合适的,如果使用这些项目远比创建执行程序(在您的情况下似乎是这样)那么昂贵。