gpt4 book ai didi

java - 在 Java 中实现多消费者多生产者的问题

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:13:39 24 4
gpt4 key购买 nike

我写了一个简单的消费者-生产者问题,它有一个阻塞队列,该队列有多个生产者和多个消费者获取并将整数放入队列。但是,当我尝试对其进行测试时,结果并不理想,例如队列的大小不正确。我不认为消费者和生产者的规模是同步的。此外,我对生产者和消费者都进行了 2 秒的 sleep ,但在测试时,它每两秒打印出 2 个生产者和 2 个消费者的结果。有谁知道我做错了什么?也许我开始错误的线程?我注释掉了我做的另一种方式,但结果仍然是错误的。

结果:

run:
Producing 425 Thread-0 size left 0
Consuming 890 Thread-3 size left 0
Consuming 425 Thread-2 size left 0
Producing 890 Thread-1 size left 0
Consuming 192 Thread-2 size left 0
Consuming 155 Thread-3 size left 0
Producing 155 Thread-1 size left 0
Producing 192 Thread-0 size left 0
Consuming 141 Thread-2 size left 1
Producing 141 Thread-0 size left 0
Producing 919 Thread-1 size left 0
Consuming 919 Thread-3 size left 0
Producing 361 Thread-1 size left 0
Producing 518 Thread-0 size left 0
Consuming 518 Thread-3 size left 0
Consuming 361 Thread-2 size left 0
Producing 350 Thread-0 size left 1
Consuming 350 Thread-3 size left 0
Consuming 767 Thread-2 size left 0
Producing 767 Thread-1 size left 0

制作人

import java.util.Random;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Producer implements Runnable {

BlockingQueue<Integer> items = new LinkedBlockingQueue<>();

public Producer(BlockingQueue<Integer> q) {
this.items = q;
}

private int generateRandomNumber(int start, int end) {
Random rand = new Random();
int number = start + rand.nextInt(end - start + 1);
return number;
}

public void run() {
for (int i = 0; i < 5; i++) {
int rand = generateRandomNumber(100, 1000);
try {
items.put(rand);
System.out.println("Producing " + rand + " " + Thread.currentThread().getName() + " size left " + items.size());
Thread.sleep(3000);
} catch (InterruptedException ex) {
Logger.getLogger(ProducerConsumer.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}

消费者

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Consumer implements Runnable {

BlockingQueue<Integer> items = new LinkedBlockingQueue<>();

public Consumer(BlockingQueue<Integer> q) {
this.items = q;
}

public void run() {
while (true) {
try {
System.out.println("Consuming " + items.take() + " " + Thread.currentThread().getName() + " size left " + items.size());
Thread.sleep(3000);
} catch (InterruptedException ex) {
Logger.getLogger(ProducerConsumer.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}

测试

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class ProducerConsumer {
public static void main(String args[]) {
BlockingQueue<Integer> items = new LinkedBlockingQueue<>();

Producer producer = new Producer(items);
Consumer consumer = new Consumer(items);
Thread t1 = new Thread(producer);
Thread t2 = new Thread(producer);
Thread t3 = new Thread(consumer);
Thread t4 = new Thread(consumer);
/*
Thread t1 = new Thread(new Producer());
Thread t2 = new Thread(new Producer());
Thread t3 = new Thread(new Consumer());
Thread t4 = new Thread(new Consumer());
*/
t1.start();
t2.start();
t3.start();
t4.start();
}
}

更新:我尝试实现可重入锁,但我的程序在锁定行停止。有什么帮助吗?消费者

import java.util.concurrent.locks.ReentrantReadWriteLock;

public class Consumer implements Runnable {

//private BlockingQueue<Integer> items = new LinkedBlockingQueue<>();
private MyBlockingQ items;

public Consumer(MyBlockingQ q) {
this.items = q;
}

public void run() {
while (true) {
items.remove();
//Thread.sleep(1000);
}
}
}

制作人

import java.util.Random;

public class Producer implements Runnable {
private MyBlockingQ items;
public Producer(MyBlockingQ q) {
this.items = q;
}

private int generateRandomNumber(int start, int end) {
Random rand = new Random();
int number = start + rand.nextInt(end - start + 1);
return number;
}

public void run() {
for (int i = 0; i < 5; i++) {
int rand = generateRandomNumber(100, 1000);
items.add(rand);
}
}
}

MyBlockingQ(共享资源)

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.logging.Level;
import java.util.logging.Logger;

public class MyBlockingQ {

private BlockingQueue<Integer> items = new LinkedBlockingQueue<>();
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();

public MyBlockingQ() {
}

public void add(Integer i) {
lock.writeLock().lock();
try {
items.put(i);
System.out.println("Producing " + i + " " + Thread.currentThread().getName() + " size left " + items.size());
} catch (InterruptedException ex) {
Logger.getLogger(ProducerConsumer.class.getName()).log(Level.SEVERE, null, ex);
} finally {
lock.writeLock().unlock();
}
}

public void remove() {
lock.writeLock().lock();
try {
int taken = items.take();
System.out.println("Consuming " + taken + " " + Thread.currentThread().getName() + " size left " + items.size());
} catch (InterruptedException ex) {
Logger.getLogger(ProducerConsumer.class.getName()).log(Level.SEVERE, null, ex);
} finally {
lock.writeLock().unlock();
}
}
}

测试

import java.util.concurrent.BlockingQueue; 
import java.util.concurrent.LinkedBlockingQueue;

public class ProducerConsumer {
public static void main(String args[]) {
MyBlockingQ items = new MyBlockingQ();

System.out.println("starting");
Thread t1 = new Thread(new Producer(items));
Thread t2 = new Thread(new Producer(items));
Thread t3 = new Thread(new Consumer(items));
Thread t4 = new Thread(new Consumer(items));
t1.start();
t2.start();
t3.start();
t4.start();
}
}

最佳答案

这两行

items.put(rand);
System.out.println("Producing " + rand + " " + Thread.currentThread().getName() + " size left " + items.size());

不同步。生产者可能将数字放入队列中,但是当队列的大小从放入队列的线程中显示出来时,消费者可能已经消费了一个数字。

关于java - 在 Java 中实现多消费者多生产者的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12795234/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com