Alachisoft.NCache.Common.yield.ThreadedYieldAdapter Maven / Gradle / Ivy
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Alachisoft.NCache.Common.yield;
import java.util.NoSuchElementException;
import java.util.concurrent.SynchronousQueue;
/**
* A class to convert methods that implement the Collector<> class into a standard Iterable<>, using a new thread created for the collection process, and a SynchronousQueue<>
* object.
*/
public class ThreadedYieldAdapter implements YieldAdapter {
/**
* Convert a method that implements the Collector<> class with a standard Iterable<>. This means that the collecting method can use complex recursive logic, but still allows
* the calling code to handle the results with a standard iterator. Results are returned immediately and do not incur overhead of being stored in a list. Calculation overhead
* is only performed for the results that are requested through the iterator.
*
* This is implemented using a new thread created for the collection process, and a SynchronousQueue<> object.
*/
public YieldAdapterIterable adapt(final Collector client) {
return new YieldAdapterIterable() {
public YieldAdapterIterator iterator() {
final SynchronousQueue synchronousQueue = new SynchronousQueue();
// Mechanism to ensure both threads don't run at the same time
final SynchronousQueue