简体   繁体   中英

Java Concurrent Collection Search

I've been programming in Java for sometime but new to concurrent programming, so bear with me!

I'm trying to develop a class that holds a group of Collection classes [eg ArrayLists] and then to find a specified value it traverses all collections at the same time, stopping all threads if it finds the given value.

I've pasted my code below and was wondering if anyone knows how within contains_multiple_collections() I catch if one of the threads returned Futures has returned true?

Thanks

Graham

public class CollectionGroup<V> extends ContainerGroup
{
//...
    public boolean contains(V value)
    {
        boolean containsValue = false;
        if (mCollections.size() == 1)
        {
            containsValue = mCollections.get(0).contains(value);
        }
        else
        {
            containsValue = contains_multiple_collections(value);
        }
        return containsValue;
    }

    private boolean contains_multiple_collections(V value)
    {
        // thread pool
        int numberProcessors = mCollections.size();
        ExecutorService es = Executors.newFixedThreadPool(numberProcessors);

        for (int i=0; i<numberProcessors; i++)
        {
            AbstractCollection<V> collection = mCollections.get(i);
            MyCallable callable = new MyCallable(collection,value);
            Future<Boolean> future = es.submit(callable);
            //...
        }

        return true;
    }

    private class MyCallable implements Callable<Boolean>
    {
        protected AbstractCollection<V> mCollection;
        protected V                     mValue;

        public MyCallable(AbstractCollection<V> collection, V value)
        {
            mCollection = collection;
            mValue      = value;
        }

        @Override
        public Boolean call() throws Exception
        {
            boolean ok = mCollection.contains(mValue);
            return ok;
        }
    } // class MyCallable

} // class CollectionGroup

contains won't stop just because you might have found the value in another thread. The only way to do this is to loop yourself.

For a CPU intensive process, the optimal number of threads is likely to be the number of cores you have. Creating too many threads adds overhead which slows down your solution.

You should also remember to shutdown() the ExecutorService when you are finished with it.

If you want to speed up the search, I would maintain a Set of all values this is likely to be 10-100x faster than using multiple threads.

You could use an ExecutorCompletionService. Just keep take()ing (take() blocks until a completed Future is present) until you get a result that is true and shutdownNow() the underlying ExecturService once you've found something. This won't immediately stop all threads once a value is found though.

You could repeatedly loop through all the futures and poll them with get , using zero or almost zero timeout, but probably a better idea is to provide a callback to all your MyCallable s, which should then call it when a match is found. The callback should then cancel all other tasks, maybe by shutting down the ExecutorService .

I suggest using a static AtomicBoolean which is set when a match is found. Each thread could then check the value before proceeding.

The issue is that your contains_multiple_collections method does not wait for the search to complete. You have two options I can think of. The first would involve some asynchronous callback implementation where the contains method does not block and perhaps takes a callback/listener object as an argument. The second is to make the contains method block until an outcome has been determined. I've outlined a sample implementation for latter approach below, it's not tested so be careful...

/*
 * contains_multiple_collections now blocks until the desired 
 * value is located or all searches have completed unsuccessfully...
 */
private boolean contains_multiple_collections(V value) {
    // thread pool
    int numberProcessors = mCollections.size();
    ExecutorService es = Executors.newFixedThreadPool(numberProcessors);

    Object lock = new Object();
    AtomicBoolean outcome = new AtomicBoolean(false);
    AtomicInteger remainingSearches = new AtomicInteger(mCollections.size());

    for (int i = 0; i < numberProcessors; i++) {
        AbstractCollection<V> collection = mCollections.get(i);
        es.submit(new MyRunnable(collection, value, lock, outcome, remainingSearches));
    }

    /* Wait for searches to run. This thread will be notified when all searches
     * complete without successfully locating the value or as soon as the
     * desired value is found.
     */
    synchronized (lock) {
        while (!outcome.get() && remainingSearches.get() > 0) {
            try {
                lock.wait();
            } catch (InterruptedException ex) {
                // do something sensible.
            }
        }
        es.shutdownNow();
    }

    return outcome.get();
}

private class MyRunnable implements Runnable {

    final AbstractCollection<V> mCollection;
    final V                     mValue;
    final Object                lock;
    final AtomicBoolean         outcome;
    final AtomicInteger         remainingSearches;

    public MyRunnable(AbstractCollection<V> mCollection, V mValue, 
            Object lock, AtomicBoolean outcome, AtomicInteger remainingSearches) {
        this.mCollection = mCollection;
        this.mValue = mValue;
        this.lock = lock;
        this.outcome = outcome;
        this.remainingSearches = remainingSearches;
    }

    public void run() {
        boolean ok = mCollection.contains(mValue);
        if (ok || remainingSearches.decrementAndGet() == 0) {
            synchronized (lock) {
                if (ok) {
                    outcome.set(true);
                }

                lock.notify();
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM