英文:
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("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 + " is prime \n");
}
}
@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("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);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论