Java: concurrent iteration of immutable iteratable

I have an immutable iteratable < x > with a large number of elements (it happens to be a list < > but it doesn't matter.)

What I want to do is start some parallel / asynchronous tasks to iterate. Using the same iterator, I want to know what interface I should use

This is an example implementation with a pending interface, quasiiteratorinterface:

public void process(Iterable<X> iterable)
{
   QuasiIteratorInterface<X> qit = ParallelIteratorWrapper.iterate(iterable);
   for (int i = 0; i < MAX_PARALLEL_COUNT; ++i)
   {
      SomeWorkerClass worker = new SomeWorkerClass(qit);
      worker.start();
   }
}

class ParallelIteratorWrapper<T> implements QuasiIteratorInterface<T>
{
   final private Iterator<T> iterator;
   final private Object lock = new Object();
   private ParallelIteratorWrapper(Iterator<T> iterator) { 
      this.iterator = iterator;
   }
   static public <T> ParallelIteratorWrapper<T> iterate(Iterable<T> iterable)
   {
      return new ParallelIteratorWrapper(iterable.iterator());
   }
   private T getNextItem()
   {
      synchronized(lock)
      {
         if (this.iterator.hasNext())
            return this.iterator.next();
         else
            return null;
      }
   }
   /* QuasiIteratorInterface methods here */
}

This is my question:

There is no point in using Iterator directly, because hasNext () and next () have synchronization problems. If others call next () before you do it, then hasNext () is useless. I like to use queue, but the only way I need is poll() > I like to use concurrentlinkedqueue to save a large number of my elements... Except that I may have to iterate over elements many times, so I can't use it

Any suggestions?

Solution

Create your own producer interface using the poll () method or an equivalent method (such as guava's supplier) There are many implementation options, but if you have an immutable random access list, you can simply maintain a thread safe monotonic counter (such as atomicinteger) and call the list Get (int), for example:

class List@R_502_1224@<T> implements @R_502_1224@<T> {
  private final AtomicInteger next = new AtomicInteger();
  private final List<T> elements; // ctor injected

  …
  public <T> get() {
    // real impl more complicated due to bounds checks
    // and what to do when exhausted
    return elements.get(next.getAndIncrement());
  }
}

This is thread safe, but you may want to return an option style thing, or null when exhausted

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>