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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T15:50:28+00:00 2026-05-24T15:50:28+00:00

I was trying to do some analysis on calling start and join simultaneously, //Starting

  • 0

I was trying to do some analysis on calling start and join simultaneously,

    //Starting and Joining 
    for (Thread thread : threadArray) {
        thread.start();
        thread.join();
    }

compared with start first and then join.

    //Starting Them
    for (Thread thread : threadArray) {
        thread.start();
    }
    //Joining Them
    for (Thread thread : threadArray) {
        thread.join();
    }

What would be the performance difference between the above two cases?.

In the first scenario, I am pretty much guaranteeing that the order of execution is sequential between threads. So if I have n threads and say each thread takes Ti time to complete the task, my total time of execution should be sum of Tis from 1 to n.

In the second scenario, I am starting off and then joining. This is the part which I am getting confused. Shouldn’t the time be almost same as above?. What I am seeing is almost double on my machine.

The entire code sample I am using is given below.

public class ThreadJoin implements Runnable {

    public void run() {
        for (int i=0;i<10000000;i++) {
            //Random mathematical stuff independent of i.
             int ran = (int) (Math.random()*1000 -34)%47;
        }
    }

    public static void main(String[] args) throws Exception {
        Thread[] threadArray = new Thread[10];
        //Creating threads and feeding them with the job
        for (int i=0;i<10;i++) {
            threadArray[i] = new Thread(new ThreadJoin());
        }
        long currentTimeMillis = System.currentTimeMillis();
        System.out.println("Started at " + currentTimeMillis);
        //Starting Them
        for (Thread thread : threadArray) {
            thread.start();
        }
        //Joining Them
        for (Thread thread : threadArray) {
            thread.join();
        }
        long currentTimeMillis2 = System.currentTimeMillis();
        System.out.println("Ended at " + currentTimeMillis2);
        System.out.println("Diff : " +( currentTimeMillis2 - currentTimeMillis));
    }

}
  • 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-24T15:50:29+00:00Added an answer on May 24, 2026 at 3:50 pm

    In theory, starting first all the threads and then joining them all should allow your 10 threads to execute concurrently (i.e. at the same time), while starting and joining the threads in one loop will make them run in parallel.

    So, in theory, the two-loop variant should be faster. Why is it actually slower (if I understand this right)?

    You are using Math.random() in your loop quite heavily. In fact, I suppose most of the work occurs in this method.
    Math.random() is a synchronized method – this means that only one thread at a time can execute it, and the other ones have to wait until the previous one is finished.

    So, you can’t really get faster than sequentially here. It actually gets slower since you have lots of context switches between your many threads, most of which will then find out they can’t continue since another thread already has the lock.

    To make your program faster, let each thread have its own java.util.Random() object, and call its nextRandom() method instead. (You might want to make sure that they are initialized with different seeds, though.)

    As mentioned in the comment from Tomek, from Java 7 on there is the ThreadLocalRandom class, which organizes such a pool of Random objects per thread and exposes the one for the current thread by its current() method. (I did never use this, so I can’t comment on the performance compared to doing this manually.)

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

Sidebar

Related Questions

I'm trying some of the ASP.NET MVC tutorials and one of them has the
I'm trying to write a function to do some often repeated analysis, and one
I'm trying to do some network analysis for a client. The provided road-network GIS
i'm trying to do age analysis on some data and need to do a
I am trying to do some analysis of the recent MLB draft with some
I'm trying to do some analysis and when writing down a list of functionalities
I am new to R and trying to do some correlation analysis on multiple
I have a table where I'm trying to pull some trend analysis from where
I'm trying to aggregate some customer order data into one table for analysis. The
I’m thinking about trying some development for the iPhone, is it possible to install

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.