Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6533989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:11:47+00:00 2026-05-25T10:11:47+00:00

I’ve been programming in Java for sometime but new to concurrent programming, so bear

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T10:11:47+00:00Added an answer on May 25, 2026 at 10:11 am

    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();
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I'm looking for suggestions for debugging... If you view this site in Firefox or
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.