简体   繁体   English

Java多线程 - Eratosthenes筛选的基本并行示例

[英]Java Multithreading - a basic parallel example of Sieve of Eratosthenes

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. 我想创建一个简单的并行Sieve of Erastosthenes Java程序,它至少比我在下面发布的串行版本更有效。

  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. 我创建了一个循环来划分4个线程的工作。 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. 如何发送变量并启动4个线程,每个线程包含部分作业。

I can propose following solution: there are 4 workers thread and 1 master thread. 我可以提出以下解决方案:有4个工作线程和1个主线程。 Worker threads get jobs from queue. 工作线程从队列中获取作业。 Job is basically 3 numbers: from, to, step. 工作基本上是3个数字:从,到,步。 Master mean while must wait until all threads a done. 掌握意味着必须等到所有线程完成。 When they're done it searches for next prime number and create 4 jobs. 当它们完成后,它会搜索下一个素数并创建4个作业。 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. 使用信号量可以实现主服务器和工作服务器之间的同步:主服务器尝试获取4个许可证,而每个工作程序在完成时释放1个许可证。

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();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM