ArrayBlockingQueue vs LinkedBlockingQueue
ทั้งคู่ implement BlockingQueue interface เก็บข้อมูลแบบ FIFO Insertion ที่ tail, Removal ที่ head
ตารางเปรียบเทียบ
| Key | ArrayBlockingQueue | LinkedBlockingQueue |
|---|---|---|
| Backing Structure | Array | Linked List |
| Bounded | ใช่ (fixed capacity) | ไม่ (unbounded โดย default) |
| Throughput | ต่ำกว่า | สูงกว่า (linked queues) |
| Locking | Single-lock double condition | putLock (insert) + takeLock (remove) แยกกัน |
รายละเอียด
- ArrayBlockingQueue: ใช้ array คงที่ เหมาะเมื่อรู้ capacity ล่วงหน้า ต้องการ bounded buffer
- LinkedBlockingQueue: ใช้ linked list ขยายได้ ไม่มีขีดจำกัด (เว้นแต่ระบุ) Throughput สูงกว่าเพราะ lock แยกสำหรับ put/take
สรุป
เลือก ArrayBlockingQueue เมื่อต้องการ bounded และ capacity คงที่ เลือก LinkedBlockingQueue เมื่อต้องการ unbounded หรือ throughput สูง