В следующем примере приведенный пример ConcurrentHashMap возвращает несовместимые значения. Разве он не должен всегда возвращать n значений?
import java.util.Scanner;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
public class ByArrayBlockingQueue {
private static int currentPrime = 0;
private static BlockingQueue<Integer> linkedBlockingQueue = new LinkedBlockingQueue<Integer>();
static Object lock = new Object();
public static void main(String[] args) throws InterruptedException {
ConcurrentHashMap<Long, Integer> primesproduced = new ConcurrentHashMap<Long, Integer>();
Scanner reader = new Scanner(System.in);
System.out.print("Enter number of threads you want to create: ");
int n = reader.nextInt();
reader.close();
ExecutorService executor = Executors.newFixedThreadPool(n);
Thread producer = new Thread(() -> {
long threadId = Thread.currentThread().getId();
int p = 0;
try {
// synchronized (lock) {
p = generateNextPrime();
linkedBlockingQueue.put(p);
primesproduced.put(threadId, p);
System.out.println("Thread " + threadId + " produced prime number " + p);
// }
} catch (InterruptedException e) {
e.printStackTrace();
}
});
Thread consumer = new Thread(() -> {
long threadId = Thread.currentThread().getId();
int p = 0;
try {
p = linkedBlockingQueue.take();
System.out.println("Thread " + threadId + " consumed the prime number " + p);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
for (int i = 0; i < n; i++) {
executor.execute(producer);
executor.execute(consumer);
}
executor.shutdown();
Thread.sleep(100);
System.out.println("\nTotal primes produced: " + primesproduced);
}
private static int generateNextPrime() {
currentPrime++;
if (currentPrime < 2) {
currentPrime = 2;
return currentPrime;
}
for (int i = 2; i < currentPrime; i++) {
if (currentPrime % i == 0) {
currentPrime++;
i = 2;
} else {
continue;
}
}
return currentPrime;
}
}
При разных запусках он возвращает непоследовательное количество элементов, и я думаю, что вместо этого он должен вернуть произведенное количество элементов. Например, при n = 10 он возвращает только шесть элементов вместо 10 элементов. Тем не менее, у производителей есть 10 различных простых чисел Например:
Enter number of threads you want to create: 10
Thread 19 produced prime number 5
Thread 21 produced prime number 7
Thread 17 produced prime number 3
Thread 23 produced prime number 11
Thread 20 consumed the prime number 2
Thread 22 consumed the prime number 3
Thread 15 produced prime number 2
Thread 19 produced prime number 13
Thread 15 consumed the prime number 17
Thread 22 consumed the prime number 13
Thread 21 consumed the prime number 7
Thread 24 consumed the prime number 5
Thread 18 consumed the prime number 11
Thread 22 consumed the prime number 29
Thread 15 produced prime number 29
Thread 19 consumed the prime number 23
Thread 20 produced prime number 23
Thread 23 produced prime number 19
Thread 16 consumed the prime number 19
Thread 17 produced prime number 17
Total primes produced: {17=17, 19=13, 20=23, 21=7, 23=19, 15=29}
Оцените помощь. Спасибо.