[英]Multi-Threaded Prime number Finder in Java
我正在制作一個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;
跟蹤總計的素數。 由於它是static
,因此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 ++交錯,您應在遞增計數時進行同步。
在多線程方面無能為力,我發現此頁面試圖解決類似問題,但是您的主要發現算法有兩個問題:
首先,在您的run()
方法中,為什么while循環中有primes[0] = 2
? 每次只需要設置一次時,它就會被設置。
其次,您跳過3,這是一個質數。 發生這種情況是因為將myNumber
設置為3,但是在檢查之前先調用getNextNumber()
。 將myNumber
初始化為1。
盡管@Saposhiente的答案是正確的,但我還是要發布適當的版本供OP考慮並找出其他小問題
線程類:
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
public class PrimeThread extends Thread {
final public static long[] primes = new long[100000];
static {
primes[0] = 2; // init 1st prime only 1 time
};
final static AtomicLong nextNumber = new AtomicLong(3L);
final static long MAX = 1000L;
public int count = 0; // non static local count
public static AtomicInteger totalCount = new AtomicInteger(); // static global count
public void run() {
long myNumber;
while ((myNumber = nextNumber.getAndAdd(2L)) <= MAX)
if (prime(myNumber)) {
primes[totalCount.incrementAndGet()] = myNumber; // note increment and get instead of get and increment
count++;
}
}
public static boolean prime(final long n) {
final long maxI = (long) Math.sqrt(n); // faster than calculation of i*i each time
for (long i = 3; i <= maxI; i += 2)
if (n%i==0) return false;
return true;
}
}
主程序:
public class MultiThreadedPrimeFinder {
static final int nThreads = 2;
public static void main(final String[] args) {
final PrimeThread[] pthreads = new PrimeThread[nThreads];
final long starttime = System.nanoTime();
for (int i = 0; i < nThreads; i++) {
pthreads[i] = new PrimeThread();
pthreads[i].start();
}
try {
for (int i = 0; i < nThreads; i++)
pthreads[i].join();
} catch (InterruptedException e) {
}
final long endtime = System.nanoTime(); // measure only actual execution, without any system calls
System.out.println("The run time is " + ((endtime - starttime) / 1000000L) + " milliseconds");
for (int i = 0; i < nThreads; i++)
System.out.println("Thread " + i + " Prime count: " + pthreads[i].count); // output each thread's count
System.out.println("Total prime count: " + PrimeThread.totalCount.get()); // output total count
for (int i = 0; i < 100; i++)
System.out.println(""+i+": "+PrimeThread.primes[i]);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.