java实现生产者和消费者
时间: 2023-05-20 08:04:25 浏览: 66
以下是C语言代码:
#include <stdio.h>
int main() {
int n = 20, i;
int fib[n];
fib[0] = 0;
fib[1] = 1;
for (i = 2; i < n; i++) {
fib[i] = fib[i-1] + fib[i-2];
}
printf("Fibonacci数列的前20个数为:\n");
for (i = 0; i < n; i++) {
printf("%d ", fib[i]);
}
return 0;
}
相关问题
用java实现生产者和消费者模式
生产者和消费者模式是一种常见的多线程编程模式,可以用于解决生产者和消费者之间的数据交换问题。在Java中,可以使用多种方式实现生产者和消费者模式,比如使用wait()和notify()方法、使用BlockingQueue等。以下是一种使用wait()和notify()方法实现的生产者和消费者模式的示例代码:
```
public class ProducerConsumerExample {
private static final int BUFFER_SIZE = 10;
private static List<Integer> buffer = new ArrayList<>(BUFFER_SIZE);
public static void main(String[] args) {
Thread producerThread = new Thread(new Producer());
Thread consumerThread = new Thread(new Consumer());
producerThread.start();
consumerThread.start();
}
static class Producer implements Runnable {
@Override
public void run() {
while (true) {
synchronized (buffer) {
while (buffer.size() == BUFFER_SIZE) {
try {
buffer.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
int item = (int) (Math.random() * 100);
buffer.add(item);
System.out.println("Produced item: " + item);
buffer.notifyAll();
}
}
}
}
static class Consumer implements Runnable {
@Override
public void run() {
while (true) {
synchronized (buffer) {
while (buffer.isEmpty()) {
try {
buffer.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
int item = buffer.remove(0);
System.out.println("Consumed item: " + item);
buffer.notifyAll();
}
}
}
}
}
```
在这个示例代码中,Producer和Consumer都是实现了Runnable接口的线程类。Producer线程不断地生成随机数,并将其添加到buffer中,如果buffer已满,则等待。Consumer线程不断地从buffer中取出数据,并打印出来,如果buffer为空,则等待。在每次修改buffer的时候,都需要获取buffer的锁,并在修改完成后调用notifyAll()方法来唤醒其他等待的线程。
至于"lua closure factory 完整代码"和"中文加密"的问题,我可以回答。但是请注意,这些问题的答案并不代表我是一个真正的AI,而只是一个由CSDN开发的模拟AI。
java实现生产者和消费者问题(附完整源码)
生产者和消费者问题是一个经典的多线程同步问题,下面是 Java 实现的示例代码:
```java
import java.util.LinkedList;
import java.util.Queue;
public class ProducerConsumerExample {
public static void main(String[] args) {
Queue<Integer> buffer = new LinkedList<>();
int maxSize = 10;
Thread producer = new Thread(new Producer(buffer, maxSize));
Thread consumer = new Thread(new Consumer(buffer));
producer.start();
consumer.start();
}
}
class Producer implements Runnable {
private Queue<Integer> buffer;
private int maxSize;
public Producer(Queue<Integer> buffer, int maxSize) {
this.buffer = buffer;
this.maxSize = maxSize;
}
public void run() {
for (int i = 0; i < 20; i++) {
try {
produce(i);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private void produce(int i) throws InterruptedException {
synchronized (buffer) {
while (buffer.size() == maxSize) {
System.out.println("Buffer is full, waiting...");
buffer.wait();
}
buffer.add(i);
System.out.println("Produced: " + i);
buffer.notifyAll();
}
}
}
class Consumer implements Runnable {
private Queue<Integer> buffer;
public Consumer(Queue<Integer> buffer) {
this.buffer = buffer;
}
public void run() {
while (true) {
try {
consume();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private void consume() throws InterruptedException {
synchronized (buffer) {
while (buffer.isEmpty()) {
System.out.println("Buffer is empty, waiting...");
buffer.wait();
}
int value = buffer.poll();
System.out.println("Consumed: " + value);
buffer.notifyAll();
}
Thread.sleep(1000);
}
}
```
这段代码中,Producer 是生产者线程,Consumer 是消费者线程,它们共享一个 buffer 队列,生产者不断往队列里添加元素,直到队列满了就等待,消费者不断从队列里取元素,直到队列为空就等待。注意到 buffer 上的锁的使用,以及 wait() 和 notifyAll() 的调用,这些都是多线程同步的基本操作。
以上是示例代码,仅供参考。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)