0

给定以下从 Cassandra 数据库中获取记录的代码片段:

QueryResult<OrderedRows<String, String, String>> result = rangeSlicesQuery.execute();
OrderedRows<String, String, String> rows = result.get();
Iterator<Row<String, String, String>> rowsIterator = rows.iterator();

如果我将 rowsIterator 对象放在定义为的 Queue 上:

BlockingQueue<Iterator<Row<String, String, String>>> queue = 
    new PriorityBlockingQueue<Iterator<Row<String, String, String>>>();

现在,如果我有一个从这个队列中读取的线程,我是否能够从队列中获取迭代器并迭代这个线程中的 OrderRows?

换句话说,是否可以将在一个线程中创建的迭代器传递给第二个线程,以便第二个线程可以迭代在第一个线程中为其创建迭代器的原始集合?

4

1 回答 1

2

Briefly, yes. It's just an object referencing your collection.

I would perhaps be wary of this. Having an iterator used in one thread presumably means that you can modify your collection in the other thread, and you run the risk of concurrent modifications unless you're careful. Perhaps iterate over a copy of your original collection ?

于 2012-12-10T09:21:37.817 回答