ภาพรวมของ LinkedBlockingDeque
LinkedBlockingDeque นำ Collection interface และ AbstractQueue มาใช้งาน ให้ขอบเขตตัวเลือกตามโหนดเชื่อมโยง สามารถกำหนดขนาดผ่าน Constructor
ตัวอย่างการใช้งาน removeLast()
ตัวอย่างนี้สาธิตเมธอด removeLast() ซึ่งนำสมาชิกที่ ท้าย (tail/last) ออกและคืนค่า โยน NoSuchElementException หาก Deque ว่าง
ขั้นตอนที่ 1-2: สร้างโปรเจกต์และ Layout
สร้างโปรเจกต์ใหม่ ใช้ Layout TextView
ขั้นตอนที่ 3: โค้ด Java
package com.example.myapplication;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import java.util.concurrent.LinkedBlockingDeque;
public class MainActivity extends AppCompatActivity {
LinkedBlockingDeque<String> linkedBlockingDeque;
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linkedBlockingDeque = new LinkedBlockingDeque<>();
final TextView actionEvent = findViewById(R.id.actionEvent);
linkedBlockingDeque.add("sai");
linkedBlockingDeque.add("ram");
linkedBlockingDeque.add("krishna");
linkedBlockingDeque.add("prasad");
linkedBlockingDeque.add("ram");
actionEvent.setText("" + linkedBlockingDeque);
actionEvent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
linkedBlockingDeque.removeLast(); // นำออกที่ท้าย โยน Exception ถ้าว่าง
actionEvent.setText("" + linkedBlockingDeque);
}
});
}
}
removeLast() vs pollLast() vs removeFirst()
| เมธอด | ตำแหน่ง | Deque ว่าง | Blocking |
|---|---|---|---|
removeLast() | ท้าย (Last) | NoSuchElementException | ไม่ |
pollLast() | ท้าย (Last) | null | ไม่ |
takeLast() | ท้าย (Last) | Block จนกว่าจะมีข้อมูล | ใช่ |
removeFirst() | หัว (First) | NoSuchElementException | ไม่ |
⚠️ ข้อควรระวัง
❌ ไม่ควรใช้ removeLast() โดยไม่ตรวจสอบ
โยน
แนะนำ: ใช้
โยน
NoSuchElementException หาก Deque ว่าง → Crash แอปแนะนำ: ใช้
pollLast() คืน null แทน (ปลอดภัยกว่า)
แนวทางปลอดภัย (Recommended)
// แบบที่ 1: ใช้ pollLast() (แนะนำ)
String removed = linkedBlockingDeque.pollLast();
if (removed != null) {
actionEvent.setText("Removed: " + removed + " | Remaining: " + linkedBlockingDeque);
} else {
actionEvent.setText("Deque is empty!");
}
// แบบที่ 2: ตรวจสอบก่อนลบ
if (!linkedBlockingDeque.isEmpty()) {
String removed = linkedBlockingDeque.removeLast();
actionEvent.setText("Removed: " + removed + " | Remaining: " + linkedBlockingDeque);
}
// แบบที่ 3: Blocking version (สำหรับ Worker Thread เท่านั้น)
// String removed = linkedBlockingDeque.takeLast(); // Block จนกว่าจะมีข้อมูล
วิธีทดสอบ
- รันแอป ดู Deque: [sai, ram, krishna, prasad, ram]
- คลิก TextView → นำ "ram" (ตัวสุดท้าย) ออก เหลือ [sai, ram, krishna, prasad]
- คลิกซ้ำจน Deque ว่าง
- คลิกอีกครั้ง → Crash (NoSuchElementException)
- ลองแก้โค้ดใช้ pollLast() จะไม่ Crash
กรณีใช้งานจริง
- Stack behavior (LIFO) - Undo/Redo, Expression evaluation
- Cache eviction - ลบรายการเก่าสุด (ถ้าเพิ่มใหม่ที่หัว)
- Buffer management - ดึงข้อมูลล่าสุดออกมาประมวลผล
ความแตกต่างกับ ConcurrentLinkedDeque
- LinkedBlockingDeque: รองรับ Blocking (takeLast, putLast), Bounded optional
- ConcurrentLinkedDeque: Non-blocking, Unbounded, Lock-free
- ทั้งคู่มี removeLast() พฤติกรรมเหมือนกัน (โยน Exception ถ้าว่าง)