Movatterモバイル変換


[0]ホーム

URL:


Tech & Media Labs
Home
RSS
Java Concurrency
  1. Java Concurrency and Multithreading Tutorial
  2. Multithreading Benefits
  3. Multithreading Costs
  4. Concurrency Models
  5. Same-threading
  6. Single-threaded Concurrency
  7. Concurrency vs. Parallelism
  8. Creating and Starting Java Threads
  9. Java Virtual Threads
  10. Race Conditions and Critical Sections
  11. Thread Safety and Shared Resources
  12. Thread Safety and Immutability
  13. Java Memory Model
  14. Java Happens Before Guarantee
  15. Java Synchronized Blocks
  16. Java Volatile Keyword
  17. CPU Cache Coherence in Java Concurrency
  18. False Sharing in Java
  19. Java ThreadLocal
  20. Thread Signaling in Java
  21. Deadlock
  22. Deadlock Prevention
  23. Starvation and Fairness
  24. Nested Monitor Lockout
  25. Slipped Conditions
  26. Locks in Java
  27. Read / Write Locks in Java
  28. Reentrance Lockout
  29. Semaphores
  30. Blocking Queues
  31. The Producer Consumer Pattern
  32. Thread Pools
  33. Thread Congestion in Java
  34. Compare and Swap
  35. Anatomy of a Synchronizer
  36. Non-blocking Algorithms
  37. Amdahl's Law
  38. Java Concurrency References

Blocking Queues

Jakob Jenkov
Last update: 2020-01-15

A blocking queue is a queue that blocks when you try to dequeue from it and the queue is empty, or if you try to enqueue items to it and the queue is already full. A thread trying to dequeue from an empty queue is blocked until some other thread inserts an item into the queue. A thread trying to enqueue an item in a full queue is blocked until some other thread makes space in the queue, either by dequeuing one or more items or clearing the queue completely.

Here is a diagram showing two threads cooperating via a blocking queue:

A BlockingQueue with one thread putting into it, and another thread taking from it.
A BlockingQueue with one thread putting into it, and another thread taking from it.

Java 5 comes with blocking queue implementations in thejava.util.concurrent package. You can read about that class in myjava.util.concurrent.BlockingQueue tutorial. Even if Java 5 comes with a blocking queue implementation, it can be useful to know the theory behind their implementation.


Blocking Queue Implementation

The implementation of a blocking queue looks similar to aBounded Semaphore. Here is a simple implementation of a blocking queue:

public class BlockingQueue {  private List queue = new LinkedList();  private int  limit = 10;  public BlockingQueue(int limit){    this.limit = limit;  }  public synchronized void enqueue(Object item)  throws InterruptedException  {    while(this.queue.size() == this.limit) {      wait();    }    this.queue.add(item);    if(this.queue.size() == 1) {      notifyAll();    }  }  public synchronized Object dequeue()  throws InterruptedException{    while(this.queue.size() == 0){      wait();    }    if(this.queue.size() == this.limit){      notifyAll();    }    return this.queue.remove(0);  }}

Notice hownotifyAll() is only called fromenqueue() anddequeue() if the queue size is equal to the size bounds (0 or limit). If the queue size is not equal to either bound whenenqueue() ordequeue() is called, there can be no threads waiting to either enqueue or dequeue items.

Next:The Producer Consumer Pattern

Jakob Jenkov

Featured Videos

Java ConcurrentMap + ConcurrentHashMap

Java Generics

Java ForkJoinPool

P2P Networks Introduction

















Copyright Jenkov Aps
Close TOC
All Tutorial Trails
All Trails
Table of contents (TOC) for this tutorial trail
Trail TOC
Table of contents (TOC) for this tutorial
Page TOC
Previous tutorial in this tutorial trail
Previous
Next tutorial in this tutorial trail
Next

[8]ページ先頭

©2009-2025 Movatter.jp