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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:45:11+00:00 2026-05-24T18:45:11+00:00

I wrote a simple code that uses multiple threads to calculate number of primes

  • 0

I wrote a simple code that uses multiple threads to calculate number of primes from 1 to N.

public static void main (String[] args) throws InterruptedException
{
    Date start;
    start = new Date();
    long startms = start.getTime();
    int number_primes = 0, number_threads =0;
    number_primes = Integer.parseInt(args[0]);
    number_threads = Integer.parseInt(args[1]);

    MakeThread[] mt = new MakeThread[number_threads];

    for(int i=1;i<=number_threads;i++)
    {
        mt[i-1] = new MakeThread(i,(i-1)*(number_primes/number_threads),i*(number_primes/number_threads));
        mt[i-1].start();
    }

    for(int i=1;i<number_threads;i++)
    {
        mt[i-1].join();
    }

    Date end = new Date();
    long endms = end.getTime();

    System.out.println("Time taken = "+(endms-startms));
}

}

As show in above, I want the final time taken to be displayed (just to measure performance for different inputs). However I noticed that when I enter a really big value of N and assign only 1 or 2 threads, the scheduler seems to override the join functionality (i.e the last print statement is displayed before other threads end). Is the kernel allowed to do this? Or do I have some bug in my code?

P.S: I have only shown a part of my code. I have a similar System.out.println at the end of the function that the newly forked threads call.

  • 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-24T18:45:14+00:00Added an answer on May 24, 2026 at 6:45 pm

    Your loop is the problem.

    for(int i=1;i<number_threads;i++)
    {
        mt[i-1].join();
    }
    

    Either you change the condition to <= or you make a less cryptic loop like this:

    for(int i=0; i < number_threads;i++){
        mt[i].join();
    }
    

    Or a for each loop:

    for(MakeThread thread : mt)
      thread.join();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Today, I wrote a simple piece of code that uses getchar() to count the
I write a simple piece of code that creates a div and assign it
I'm trying to think of clever, clear, and simple ways to write code that
I've wrote this simple piece of code. And I have a slight problem with
I needed some simple string encryption, so I wrote the following code (with a
I wrote a small sample of code in C# to capture selected text from
Below is a very simple bit of code that mimics a class structure in
Im trying to build a simple AJAX script that uses a form to send
We have an app that uses simple one way binding with a GridView to
Currently, I'm working on a simple custom button that uses user supplied images as

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.