Série: Java Old
java
53 linhas
· Atualizado 2026-02-04
BlockingQueueDemo.java
Java_Old/advanced/concurrency_course/module03/src/concurrency/module03/BlockingQueueDemo.java
package concurrency.module03;
import java.util.concurrent.*;
/**
* Producer-consumer pattern using {@link java.util.concurrent.BlockingQueue}.
*
* <p><b>BlockingQueue:</b> put(item) blocks when the queue is full (bounded);
* take() blocks when the queue is empty. This provides natural backpressure:
* a fast producer is slowed down when the consumer can't keep up (queue full).
*
* <p>Here we use {@link java.util.concurrent.ArrayBlockingQueue} with capacity 10.
* One thread produces 20 integers (put); the other consumes 20 (take). When the
* queue is full, put blocks until the consumer takes an item; when empty, take
* blocks until the producer puts one.
*/
public class BlockingQueueDemo {
/**
* Starts one producer and one consumer sharing a bounded queue of capacity 10.
* Producer puts 0..19; consumer takes 20 items. put/take handle blocking and
* interrupt response.
*
* @param args unused
* @throws InterruptedException if interrupted during join
*/
public static void main(String[] args) throws InterruptedException {
BlockingQueue<Integer> queue = new ArrayBlockingQueue<>(10);
Thread producer = new Thread(() -> {
try {
for (int i = 0; i < 20; i++) {
queue.put(i); // Blocks if queue is full
System.out.println("Produced " + i);
}
} catch (InterruptedException e) { Thread.currentThread().interrupt(); }
});
Thread consumer = new Thread(() -> {
try {
for (int i = 0; i < 20; i++) {
int v = queue.take(); // Blocks if queue is empty
System.out.println("Consumed " + v);
}
} catch (InterruptedException e) { Thread.currentThread().interrupt(); }
});
consumer.start();
producer.start();
producer.join();
consumer.join();
}
}
Artigos relacionados
Java Old
java
Atualizado 2026-02-03
Main.java
Main.java — java source code from the Java Old learning materials (Java_Old/Main.java).
Ler artigo →
Java Old
java
Atualizado 2026-02-03
Student.java
Student.java — java source code from the Java Old learning materials (Java_Old/Student.java).
Ler artigo →
Java Old
java
Atualizado 2026-02-03
01_Generics.java
01_Generics.java — java source code from the Java Old learning materials (Java_Old/advanced/01_Generics.java).
Ler artigo →
Java Old
java
Atualizado 2026-02-03
02_Collections.java
02_Collections.java — java source code from the Java Old learning materials (Java_Old/advanced/02_Collections.java).
Ler artigo →
Java Old
java
Atualizado 2026-02-03
03_Streams.java
03_Streams.java — java source code from the Java Old learning materials (Java_Old/advanced/03_Streams.java).
Ler artigo →
Java Old
java
Atualizado 2026-02-03
CollectionsExample.java
CollectionsExample.java — java source code from the Java Old learning materials (Java_Old/advanced/CollectionsExample.java).
Ler artigo →