繁体   English   中英

4 线程查找素数

[英]4 thread find prime number

我尝试使用 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]);
      }
   }
}

正如@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但它们不应用作共享数据的索引。

暂无
暂无

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

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