我有生产者 - 消费者模式。 1个生产者(做一些工作)和7个消费者(8个核心机器上)。制作人打算一次下载7个文件(2000年以后)并等待7个线程处理它们,然后继续下一个7.设置大致如下:
ExecutorService threadPool = Executors.newFixedThreadPool(8);
int pollWait = 4;
int queSize = 7;
Broker broker = new Broker(queSize,pollWait);
Broker实现了一个容量为7的LinkedBlockingQueue。
Producer producer = new Producer();
producer.setBroker(broker);
Future<Integer> producerStatus = threadPool.submit(producer);
int numConsumerThreads = 7;
ArrayList<Future<Integer>> consumers = new ArrayList<Future<Integer>>();
for(int c=0; c < numConsumerThreads;c++)
{
String threadName = "consumer-"+(c+1);
Consumer consumer = new Consumer(threadName);
consumer.setBroker(broker);
Future<Integer> consumerStatus = threadPool.submit(consumer);
consumers.add(consumerStatus);
}
if(producerStatus.isDone())
{
Integer numFilesRead = producerStatus.get();
System.out.println("[INFO] Total number of files downloaded by producer: " + numFilesRead);
}
int k=0,numIndexedByThread=0;
while(!consumers.isEmpty())
{
final Iterator<Future<Integer>> i = consumers.iterator();
while (i.hasNext())
{
Future<Integer> f = i.next();
if (f.isDone())
{
i.remove();
numIndexedByThread = f.get();
k += numIndexedByThread;
}
}
}
System.out.println("[INFO] Total number of files indexed: " + k);
threadPool.shutdown();
我看到的问题是程序没有执行。
答案 0 :(得分:1)
以下行阻止并序列化结果的整个处理:
Integer numConsumedByThread = consumerStatus.get();
正确的习惯用法是检查每一个isDone()
并仅在.get()
返回true时调用isDone()
,否则.get()
会阻止循环,直到返回结果为止
while(!listOfFutures.isEmpty())
{
final Iterator<Future> i = listofFutures.iterator;
while (i.hasNext())
{
Future f = i.next();
if (f.isDone())
{
i.remove();
// call .get() and process the completed result
}
}
}
的JavaDocs