java - Java 中的多线程素数查找器

标签 java multithreading

我正在制作一个java程序,我必须找到所有素数以及素数的数量最多2亿。我必须使用所有线程共享的静态全局变量来进行试除法,以保存下一个要检查的数字是否为素数。当它找到质数时,会将其添加到数组中,然后在完成后显示该数组。这是我到目前为止所拥有的,我的所有线程都显示与素数总数相同的素数数量,任何人都可以帮助解决这个问题。

主要-

//*import java.util.Scanner;
    public class MultiThreadedPrimeFinder {
    static final int nThreads = 2;

    public static void main(String[] args) throws InterruptedException{
        int t;
        int total = 0;
        PrimeThread[] pthreads = new PrimeThread[nThreads];
        //*Scanner kb = new Scanner(System.in);
        //*System.out.println("Enter a Positive Integer: ");
        //*long num = kb.nextLong();
        long starttime, endtime, runtime, a = 0;
        starttime = System.currentTimeMillis();
        for(int i = 0; i <10000000; i ++)
            a+=i;
        for (t=0; t<nThreads; t++)
        {
            pthreads[t] = new PrimeThread();
            pthreads[t].start();
        }

        for (t=0; t<nThreads; t++)
        {
            pthreads[t].join();
            System.out.println("Thread "+t
                    +"  Prime count: "+ pthreads[t].count);
        }
        total = PrimeThread.count;
        System.out.println("Total prime count: "+total);
        for (int i=0;i<100; i++)
            System.out.println(""+i+": "+PrimeThread.primes[i]);
        endtime = System.currentTimeMillis();
        runtime = endtime - starttime;
        System.out.println("The run time is " +runtime +" milliseconds");

    }

    }

类 -

public class PrimeThread extends Thread{
static long nextNumber=3;
static final long max = 1000;
public static int count=0;
public long thread = 100;
public static long[] primes = new long[100000]; 


public void run() {
    long myNumber;
    while ((myNumber=getNextNumber())<=max) {
        primes[0] = 2;
        if (prime(myNumber)) {

                primes[count++] = myNumber;
            }
        }
    }


public static synchronized long getNextNumber() {
    long n = nextNumber;
    nextNumber +=2;
    return n;
}

public boolean prime(long n) {
    int i;

    for (i=3; i * i<=n; i+=2)
        if (n%i==0) return false;
    return true;
}
}

输出看起来像这样

Thread 0  Prime count: 167
Thread 1  Prime count: 167
Total prime count: 167
0: 2
1: 5
2: 7
3: 11
4: 13
5: 17
6: 19
7: 23
8: 29
9: 31
10: 37
11: 41
12: 43
13: 47
14: 53
15: 59
16: 61
17: 67
18: 71
19: 73
20: 79
21: 83
22: 89
23: 97
24: 101
25: 103
26: 107
27: 109
28: 113
29: 127
30: 131
31: 137
32: 139
33: 149
34: 151
35: 157
36: 163
37: 167
38: 173
39: 179
40: 181
41: 191
42: 193
43: 197
44: 199
45: 211
46: 223
47: 227
48: 229
49: 233
50: 239
51: 241
52: 251
53: 257
54: 263
55: 269
56: 271
57: 277
58: 281
59: 283
60: 293
61: 307
62: 311
63: 313
64: 317
65: 331
66: 337
67: 347
68: 349
69: 353
70: 359
71: 367
72: 373
73: 379
74: 383
75: 389
76: 397
77: 401
78: 409
79: 419
80: 421
81: 431
82: 433
83: 439
84: 443
85: 449
86: 457
87: 461
88: 463
89: 467
90: 479
91: 487
92: 491
93: 499
94: 503
95: 509
96: 521
97: 523
98: 541
99: 547
The run time is 17 milliseconds

最佳答案

你有

public static int count=0;

它跟踪获得的素数总数。由于它是静态,因此pthreads[0].count == pthreads[1].count == PrimeThread.count。要查看各个线程检索到的素数,请添加一个实例计数器:

public int myCount = 0;
....
primes[count++] = myNumber;
myCount++;
...
System.out.println("Thread "+t
        +"  Prime count: "+ pthreads[t].myCount);

此外,为了防止 count++ 交错,您应该在递增它时进行同步。

关于java - Java 中的多线程素数查找器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21666998/

相关文章:

Java:PriorityQueue 从自定义比较器返回错误的顺序?

java - 静态数组如何存储在 Java 内存中?

java - 比较数组和 arrayList 索引

Java 以编程方式扩展

python - 导入锁在 pypy 中不能跨线程工作

java - 将JPanels依次垂直添加到JFrame中,每个水平接触边框

iphone - mergeChangesFromContextDidSaveNotification 花了将近一分钟

java - Spring集成-入站 channel 适配器是多线程的吗?

c# - 字节分配不一致

multithreading - Delphi 表单创建无需卡住主线程