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

  • Home
  • SEARCH
  • 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 8560791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:21:28+00:00 2026-06-11T16:21:28+00:00

I have a thread with the following form: each execution of each thread is

  • 0

I have a thread with the following form:

  1. each execution of each thread is supposed to run a function in the class. That function is completely safe to run by itself. The function returns a value, say an int.

  2. After all threads have been executed, the function values need to be accumulated.

So, it goes (in pseudo-code) something like that:

  a = 0
  for each i between 1 to N
      spawn a thread independently and call the command v = f(i)
      when thread finishes, do safely: a = a + v
  end

I am not sure how to use Java in that case.

The problem is not creating the thread, I know this can be done using

new Thread() { 
   public void run() { 
     ... 
   } 
} 

the problem is accumulating all the answers.

Thanks for any info.

  • 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-11T16:21:29+00:00Added an answer on June 11, 2026 at 4:21 pm

    I would probably do something like:

     public class Main {
         int a = 0;
         int[] values;
         int[] results;
    
         public Main() {
             // Init values array
    
             results = new int[N];
         }
    
         public int doStuff() {
             LinkedList<Thread> threads = new LinkedList<Thread>();
    
             for (final int i : values) {
                 Thread t = new Thread() {
                     public void run() {
                         accumulate(foo(i));
                     }
                 };
    
                 threads.add(t);
                 t.start();
              }
    
              for (Thread t : threads) {
                  try {
                      t.join();
                  } catch (InterruptedException e) {
                      // Act accordingly, maybe ignore?
                  }
              }
    
              return a;
         }
    
         synchronized void accumulate(int v) {
              // Synchronized because a += v is actually
              //    tmp = a + v;
              //    a = tmp;
              // which can cause a race condition AFAIK
              a += v;
         }
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this thread that runs code of the following form where strm1 in
I have the following thread code which executes correct first time. After that from
I have the following thread: class LemonadeMainMenuThread extends Thread { private SurfaceHolder mSurfaceHolder; private
I have a thread that does the following: 1) Do some work 2) Wait
I have the same problem that is listed in the following thread. WSDL first
Assume I have one threadpool and each thread is running following method: void runMe(HashMap
I have the following function call from a thread: Thread Move = new Thread(){
I have the following code in a Runnable that gets passed to a thread
I have a config file that is in the following form: protocol sample_thread {
Let's say I want to create a collection class that is thread-safe by default.

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.