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 9052095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T13:04:55+00:00 2026-06-16T13:04:55+00:00

I am playing around with Java lacthes and wrote a piece of code to

  • 0

I am playing around with Java lacthes and wrote a piece of code to calculate some stuff in a parallel manner. The data, basically consists of a Map that each map has a list of numbers as value. My goal is to sum up all the values for all keys in this map.
To start with, I thought it is a good idea to get the sum of all values for each key in a separate thread (each key has its own separate thread) and at the end I add up what is returned from each thread to get the total sum. I use a simple CounDownLatch in my code:

public static void main(String[] args) throws InterruptedException, ExecutionException
    {
        //final CountDownLatch startGate = new CountDownLatch(1);
        final CountDownLatch endGate = new CountDownLatch(3);


        Map> data =  new HashMap>();
        Set summedData = new HashSet();
        // populate the map with data
        populateMap(data);
        //separate the data with keys
        //send each list to a new thread to process

        for (String key : data.keySet())
        {
            final List list = data.get(key);
            System.out.println(list);

            //create a FutureTask
            FutureTask future = new FutureTask(new Callable()
            {

                @Override
                public Integer call() throws Exception
                {
                    int sum = new Mapx(list).getSum();
                    endGate.countDown();
                    return sum;
                }
            });

            Thread t = new Thread(future);
            t.start();
            System.out.println(t.getState());
            summedData.add(future.get());
        }
        //keep the main method on hold until all the thread do their job
        endGate.await();
        //send to reduce
        int total = new Reduce(summedData).addAll();
        System.out.println(total);

    }

My expectation is that, if the calculation of summing up all the items in a list for a key takes long, the thread running for this key, runs in the background and another thread for the next key starts to calculate. In other words, the calculations be done on in parallel. However, i see that this is not the case, and threads are run in serial. Can someone help me how I can achieve my goal and make this piece of code parallel?

  • 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-06-16T13:04:56+00:00Added an answer on June 16, 2026 at 1:04 pm

    You call future.get() from within the loop. This method will block until the result is calculated, and therefore the loop will not continue until the other thread is done calculating, resulting in the serialized behavior you get.

    To get the behavior you desire, you should have a loop launching all futureTasks on threads, and then a loop getting the results from all the FutureTasks using get().

    You’re also probably better off submitting a Callable to an ExecutorService (see Executors), and the ExecutorService will make the Future for you.

    In fact launching calculations in parrallel and waiting for their results is what an ExectorService‘s invokeAll() method does for you.

    The use of CountDownLatch is superfluous, as the Future objects already have the necessary synchronization behavior on board.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am just playing around with some java programming and so I wanted a
i'm playing around with a piece of code i found on the web and
Just playing around with java trying to learn it etc. Here is my code
I've been playing around with WindowBuilder Pro for Java Swing layouts, and encountered some
I'm playing around with some code katas and trying to get a better understanding
I'm playing around with Java's reflection API and trying to handle some fields. Now
I'm just playing around with JSP. I just wanted to test some <jsp:useBean> stuff,
I was playing around with classLoaders in Java and noticed a strange thing. If
Completely new to java and I have been playing around with regex in a
I'm beginner in Android (and java), and I'm just playing around and want to

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.