java多线程中的质数问题

huangapple go评论78阅读模式
英文:

java Prime Number Problem in Multiple Thread

问题

我的代码如下:

public class Demo {

   public static void main(String[] args) throws InterruptedException {
      primeGen prime = new primeGen(10, 50);

      Thread th1 = new Thread(prime);
      th1.setName("Thread1");

      Thread   th2 = new Thread(prime);
      th2.setName("Thread2");

      th1.start();
      th2.start();
   }
}

class primeGen implements Runnable {
   private int N1,N2;
   private volatile int i;

   public primeGen(int n1, int n2) {
        this.N1 = n1;
        this.N2 = n2;
        i = N1;
   }

   private boolean isPrime(int N) {
      for(int j = 2; j <= N / 2; j++) {
         if(0 == N % j) return false;
      }
      return true;
   }

   private void primeRange(int N1 , int N2){
   
      Thread th = Thread.currentThread();

      for(; i <= N2; i++) {

      if(isPrime(i))
        System.out.print(th.getName() + " " + i + " 是素数 \n");
      }
   }

   @Override
   public void run() {
      primeRange(N1 , N2);
   }
}
当前控制台输出:

 - Thread1 11 是素数
 - Thread1 13 是素数
 - Thread2 13 是素数
 - Thread2 17 是素数
 - Thread2 19 是素数
 - Thread2 23 是素数
 - Thread2 29 是素数
 - Thread2 31 是素数
 - Thread2 37 是素数
 - Thread2 41 是素数
 - Thread2 43 是素数
 - Thread2 47 是素数

> 为什么会有一些素数被多个线程重复输出,如何防止这种情况?
英文:

My code looks like this:

public class Demo {

   public static void main(String[] args) throws InterruptedException {
      primeGen prime = new primeGen(10, 50);

      Thread th1 = new Thread(prime);
      th1.setName(&quot;Thread1&quot;);

      Thread   th2 = new Thread(prime);
      th2.setName(&quot;Thread2&quot;);

      th1.start();
      th2.start();
   }
}

class primeGen implements Runnable {
   private int N1,N2;
   private volatile int i;

   public primeGen(int n1, int n2) {
        this.N1 = n1;
        this.N2 = n2;
        i = N1;
   }

   private boolean isPrime(int N) {
      for(int j = 2; j &lt;= N / 2; j++) {
         if(0 == N % j) return false;
      }
      return true;
   }

   private void primeRange(int N1 , int N2){
   
      Thread th = Thread.currentThread();

      for(; i &lt;= N2; i++) {

      if(isPrime(i))
        System.out.print(th.getName() + &quot; &quot; + i + &quot; is prime \n&quot;);
      }
   }

   @Override
   public void run() {
      primeRange(N1 , N2);
   }
}

Current output of the console:

  • Thread1 11 is prime
  • Thread1 13 is prime
  • Thread2 13 is prime
  • Thread2 17 is prime
  • Thread2 19 is prime
  • Thread2 23 is prime
  • Thread2 29 is prime
  • Thread2 31 is prime
  • Thread2 37 is prime
  • Thread2 41 is prime
  • Thread2 43 is prime
  • Thread2 47 is prime

> Why are some prime numbers repeated by multiple threads and how can i prevent this?

答案1

得分: 1

Synchronise isPrime and primeRange to avoid more than one threads accessing them at the same time. Learn more from here.

public class Demo {

    public static void main(String[] args) throws InterruptedException {
        primeGen prime = new primeGen(10, 50);
        Thread th1 = new Thread(prime);
        th1.setName("Thread1");
        Thread th2 = new Thread(prime);
        th2.setName("Thread2");
        th1.start();
        th2.start();
    }
}

class primeGen implements Runnable {
    private int N1, N2;
    private volatile int i;

    public primeGen(int n1, int n2) {
        this.N1 = n1;
        this.N2 = n2;
        i = N1;
    }

    private synchronized boolean isPrime(int N) {
        for (int j = 2; j <= N / 2; j++) {
            if (0 == N % j) {
                return false;
            }
        }
        return true;
    }

    private synchronized void primeRange(int N1, int N2) {
        Thread th = Thread.currentThread();
        for (; i <= N2; i++) {
            if (isPrime(i)) {
                System.out.print(th.getName() + " " + i + " is prime \n");
            }
        }
    }

    @Override
    public void run() {
        primeRange(N1, N2);
    }
}
英文:

Synchronise isPrime and primeRange to avoid more than one threads accessing them at the same time. Learn more from here.

public class Demo {
public static void main(String[] args) throws InterruptedException {
primeGen prime = new primeGen(10, 50);
Thread th1 = new Thread(prime);
th1.setName(&quot;Thread1&quot;);
Thread th2 = new Thread(prime);
th2.setName(&quot;Thread2&quot;);
th1.start();
th2.start();
}
}
class primeGen implements Runnable {
private int N1, N2;
private volatile int i;
public primeGen(int n1, int n2) {
this.N1 = n1;
this.N2 = n2;
i = N1;
}
private synchronized boolean isPrime(int N) {
for (int j = 2; j &lt;= N / 2; j++) {
if (0 == N % j) {
return false;
}
}
return true;
}
private synchronized void primeRange(int N1, int N2) {
Thread th = Thread.currentThread();
for (; i &lt;= N2; i++) {
if (isPrime(i)) {
System.out.print(th.getName() + &quot; &quot; + i + &quot; is prime \n&quot;);
}
}
}
@Override
public void run() {
primeRange(N1, N2);
}
}

huangapple
  • 本文由 发表于 2020年4月5日 03:53:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/61033947.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定