【问题标题】:4 thread find prime number4线程查找素数
【发布时间】:2016-04-28 22:09:26
【问题描述】:

我尝试使用 4 个均匀平衡的线程来查找从 10 到 30 的素数。我想知道每个线程有多少个素数,总共有多少个,并打印出素数。我运行了几次程序,每次的输出都不一样。有人可以帮我找出问题所在。

public class Prime extends Thread    
{
   int thread;
   static long max=30;
   static long min=10;
   static long[] primes = new long[100];
   static int a=0;
   static int b=0;
   static int c=0;
   static int d=0;

   public Prime(int threadID)
   {
      thread = threadID;
   }
   public void run()
   {
      for(long i = min; i<=max; i++){
         if(isPrime(i)){
            if(thread ==1){
               if(i<=15){
                  primes[a++] = i;
               }          
            }
            if(thread ==2){
               if(i>15 && i <=20){
                  primes[b++] = i;
               }
            }
            if(thread ==3){
               if(i>20 && i<=25){
                  {
                     primes[c++] = i;
                  }
               }
            }
            if(thread ==4){
               if(i>25){
                  primes[d++] = i;
               }
            }
         }
      } 
      if(thread ==1){System.out.println("Thread 1 contains "+a+" prime numbers");}
      if(thread ==2){System.out.println("Thread 2 contains "+b+" prime numbers");}
      if(thread ==3){System.out.println("Thread 3 contains "+c+" prime numbers");}
      if(thread ==4){System.out.println("Thread 4 contains "+d+" prime numbers");}
   }

   public static boolean isPrime(long n) {
      for (int i = 2; i < Math.sqrt(n); i++) {         
         if (n % i == 0) {
            return false;
         }
      }
      return true;
   }

   public static void main(String[] arg)
   {
      Thread th1 = new Prime(1);
      Thread th2 = new Prime(2);
      Thread th3 = new Prime(3);
      Thread th4 = new Prime(4);

      th1.start();
      th2.start();
      th3.start();
      th4.start();

      try{th1.join();}
      catch(InterruptedException ie){}
      try{th2.join();}
      catch(InterruptedException ie){}
      try{th3.join();}
      catch(InterruptedException ie){}
      try{th4.join();}
      catch(InterruptedException ie){}

      int total = a+b+c+d;
      System.out.println("Total number of prime: "+total);
      for (int i=0;i<10; i++){
         System.out.println(""+i+": "+Prime.primes[i]);
      }
   }
}

【问题讨论】:

  • 呃,你的线程都在写入同一个数组中的重叠点...?

标签: java multithreading primes


【解决方案1】:

正如@Louis 在对您的问题的评论中提到的那样,您的所有线程都相互覆盖。

当 Thread1 将它的工作放在 primes[0] 中时,其他线程不会被通知,然后也将它们的工作放在 primes[0] 中(这会覆盖已经存在的工作)。你得到不同的输出主要是因为线程运行的顺序是“随机的”。

一个简单的解决方案是不为每个线程(a、b、c、d)设置索引,而是使用来自java.util.concurrent.atomic.AtomicIntegerAtomicInteger

如何使用AtomicInteger的简短示例

import java.util.concurrent.atomic.AtomicInteger;

public class Prime extends Thread    
{
   int thread;
   static long max=30;
   static long min=10;
   static long[] primes = new long[100];
   static AtomicInteger index = new AtomicInteger(0);

   public Prime(int threadID)
   {
      thread = threadID;
   }
   public void run()
   {
      for(long i = min; i<=max; i++){
         if(isPrime(i)){
            if(thread ==1){
               if(i<=15){
                  primes[index.getAndAdd(1)] = i;
               }          
            }
            if(thread ==2){
               if(i>15 && i <=20){
                  primes[index.getAndAdd(1)] = i;
               }
            }

如果您想计算每个线程使用了多少素数,那么您仍然可以使用您的a,b,c,d,但它们不应该用作共享数据的索引。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-08
    • 2013-05-28
    • 2011-04-26
    • 2021-09-23
    • 1970-01-01
    • 1970-01-01
    • 2022-01-12
    • 1970-01-01
    相关资源
    最近更新 更多