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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:09:18+00:00 2026-06-15T16:09:18+00:00

I would like to create a simple parallel Sieve of Erastosthenes Java program, that

  • 0

I would like to create a simple parallel Sieve of Erastosthenes Java program, that would be at least a bit more effective then a serial version I’ve posted below.

  public void runEratosthenesSieve(int upperBound) {
      int upperBoundSquareRoot = (int) Math.sqrt(upperBound);
      boolean[] isComposite = new boolean[upperBound + 1];
      for (int m = 2; m <= upperBoundSquareRoot; m++) {
            if (!isComposite[m]) {
                  System.out.print(m + " ");
               int threads=4;
               for (int n=1; n<=threads; n++) {
                  int job;
                  if (n==1) {job = m * m;} else {job = (n-1)*upperBound/threads;}
                  int upToJob = n*upperBound/threads; 
                   for (int k = job;  k <= upToJob; k += m)
                  {
                       isComposite[k] = true;
                  }               
               } 
            }
      }
      for (int m = upperBoundSquareRoot; m <= upperBound; m++)
            if (!isComposite[m])
                  System.out.print(m + " ");
  }

I have created a loop for dividing work for 4 threads. Though I don’t know how to make actual thread code from it. How to send variables and start 4 threads with part of job for each.

  • 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-15T16:09:20+00:00Added an answer on June 15, 2026 at 4:09 pm

    I can propose following solution: there are 4 workers thread and 1 master thread. Worker threads get jobs from queue. Job is basically 3 numbers: from, to, step. Master mean while must wait until all threads a done. When they’re done it searches for next prime number and create 4 jobs. Synchronization between master and workers can be achieved using Semaphore: master tries to acquire 4 permits while every worker releases 1 permit when it’s done.

    public class Sieve {
    
        // Number of workers. Make it static for simplicity.
        private static final int THREADS = 4;
    
        // array must be shared between master and workers threads so make it class property.
        private boolean[] isComposite;
        // Create blocking queue with size equal to number of workers.
        private BlockingQueue<Job> jobs = new ArrayBlockingQueue<Job>(THREADS);
        private Semaphore semaphore = new Semaphore(0);
        // Create executor service in order to reuse worker threads. 
        // we can use just new Thread(new Worker()).start(). But using thread pools more effective.
        private ExecutorService executor = Executors.newFixedThreadPool(THREADS);
    
        public void runEratosthenesSieve(int upperBound) {
            int upperBoundSquareRoot = (int) Math.sqrt(upperBound);
            isComposite = new boolean[upperBound + 1];
    
            // Start workers.
            for (int i = 0; i < THREADS; i++) {
                executor.submit(new Worker());
            }
            for (int m = 2; m <= upperBoundSquareRoot; m++) {
                if (!isComposite[m]) {
                    System.out.print(m + " ");
                    for (int n=1; n<= THREADS; n++) {
                        int from;
                        if (n == 1) {
                            from = m * m;
                        } else {
                            from = (n-1)*upperBound/THREADS;
                        }
                        Job job = new Job(from, n*upperBound/threads, m);
                        // Submit job to queue. We don't care which worker gets the job.
                        // Important only that only 1 worker get the job. But BlockingQueue does all synchronization for us.
                        jobs.put(job);
                    }
                    // Wait until all jobs are done.
                    semaphore.acquire(THREADS);
                }
            }
            for (int i = 0; i < n; i++) {
                // put null to shutdown workers.
                jobs.put(null);
            }
            for (int m = upperBoundSquareRoot; m <= upperBound; m++) {
                if (!isComposite[m]) {
                    System.out.print(m + " ");
                }
            }
        }
    
        private class Job {
            public int from, to, step;
    
            public Job(int from, int to, int step) {
                this.from = from;
                this.to = to;
                this.step = step;
            }
        }
    
        private Worker implements Runnable {
            while (true) {
                Job job = jobs.take();
                // null means workers must shutdown
                if (job == null) {
                    return;
                }
                for (int i = job.from; i <= job.to; i += job.step) {
                    isComposite[i] = true;
                }
                // Notify master thread that a job was done.
                semaphore.release();
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to create a simple XMPP client in java that shares his
I would like to create a simple program that would output the atomic mass
I would like to create a simple control that inherits from HeaderedContentControl, and has
I would like to create a simple content slider that would slide content continuously
So here's the dealio. I would like to create a simple batch file that'll
I have a class that I would like to create a simple example on
I'm new to REST on Java and I would like to create simple REST
I have a very simple xml file that I would like to create a
I would like to create a simple chat room like GUI interface that will
I would like to create a simple Blackjack GUI in Java. I know the

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.