Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 4线程查找素数_Java_Multithreading_Primes - Fatal编程技术网

Java 4线程查找素数

Java 4线程查找素数,java,multithreading,primes,Java,Multithreading,Primes,我尝试使用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;

我尝试使用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]);
      }
   }
}
public类Prime扩展线程
{
内螺纹;
静态长最大值=30;
静态长min=10;
静态长[]素数=新长[100];
静态int a=0;
静态int b=0;
静态int c=0;
静态int d=0;
公共素数(int-threadID)
{
线程=线程ID;
}
公开募捐
{

对于@Louis在对您的问题的评论中提到的(long i=min;i),您的所有线程都在相互覆盖

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

一个简单的解决方案是,不为每个线程(A、b、c、d)建立索引,而是使用
java.util.concurrent.AtomicInteger
中的
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;
               }
            }
导入java.util.concurrent.AtomicInteger;
公共类素数扩展线程
{
内螺纹;
静态长最大值=30;
静态长min=10;
静态长[]素数=新长[100];
静态AtomicInteger索引=新的AtomicInteger(0);
公共素数(int-threadID)
{
线程=线程ID;
}
公开募捐
{

对于(long i=min;iUh),您的线程都在写入同一数组中的重叠点。。。?