ภาพรวมของ ConcurrentLinkedDeque
ConcurrentLinkedDeque เป็น Deque แบบไม่มีขอบเขต (unbounded) บนโหนดเชื่อมโยง รองรับหลายเธรด (thread-safe) อย่างปลอดภัย
ตัวอย่างการใช้งาน removeFirst()
ตัวอย่างนี้สาธิตเมธอด removeFirst() ซึ่งนำสมาชิกที่ หัว (head/front) ออกและคืนค่า โยน 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.ConcurrentLinkedDeque;
public class MainActivity extends AppCompatActivity { // แก้: MainActivityextends
ConcurrentLinkedDeque<String> concurrentLinkedDeque;
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
concurrentLinkedDeque = new ConcurrentLinkedDeque<>();
final TextView actionEvent = findViewById(R.id.actionEvent); // แก้: TextViewactionEvent
concurrentLinkedDeque.add("sai");
concurrentLinkedDeque.add("ram");
concurrentLinkedDeque.add("krishna");
concurrentLinkedDeque.add("prasad");
actionEvent.setText("" + concurrentLinkedDeque);
actionEvent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
concurrentLinkedDeque.removeFirst(); // นำออกที่หัว โยน Exception ถ้าว่าง
actionEvent.setText("" + concurrentLinkedDeque);
}
});
}
}
🔧 การแก้ไขจากต้นฉบับ
MainActivityextends→MainActivity extendsTextViewactionEvent→TextView actionEvent- เพิ่ม Generic Type
<String>
removeFirst() ใน ConcurrentLinkedDeque
- นำสมาชิกหัวออกและคืนค่า
- โยน
NoSuchElementExceptionหาก Deque ว่าง - Thread-safe - ปลอดภัยสำหรับหลายเธรดเรียกพร้อมกัน
- Non-blocking - ไม่รอ (ต่างจาก LinkedBlockingDeque.takeFirst())
เปรียบเทียบเมธอดนำออกที่หัว
| เมธอด | คืนค่า | Deque ว่าง | Blocking |
|---|---|---|---|
removeFirst() | E | NoSuchElementException | ไม่ |
pollFirst() | E | null | ไม่ |
pop() | E | NoSuchElementException | ไม่ |
takeFirst() | E | Block จนกว่าจะมีข้อมูล | ใช่ (เฉพาะ BlockingDeque) |
⚠️ ข้อควรระวัง
❌ ไม่ควรใช้ removeFirst() บน UI Thread โดยไม่ตรวจสอบ
อาจทำให้แอป Crash หาก Deque ว่าง ควรใช้
อาจทำให้แอป Crash หาก Deque ว่าง ควรใช้
pollFirst() แทน
แนวทางปลอดภัย
// แบบที่ 1: ใช้ pollFirst() (แนะนำ)
String removed = concurrentLinkedDeque.pollFirst();
if (removed != null) {
actionEvent.setText("Removed: " + removed + " | " + concurrentLinkedDeque);
} else {
actionEvent.setText("Deque is empty!");
}
// แบบที่ 2: ตรวจสอบก่อนลบ
if (!concurrentLinkedDeque.isEmpty()) {
String removed = concurrentLinkedDeque.removeFirst();
actionEvent.setText("Removed: " + removed + " | " + concurrentLinkedDeque);
}
// แบบที่ 3: try-catch (ไม่แนะนำสำหรับ control flow)
try {
String removed = concurrentLinkedDeque.removeFirst();
actionEvent.setText("Removed: " + removed + " | " + concurrentLinkedDeque);
} catch (NoSuchElementException e) {
actionEvent.setText("Deque is empty!");
}
วิธีทดสอบ
- รันแอป ดู Deque: [sai, ram, krishna, prasad]
- คลิก TextView → นำ "sai" ออก เหลือ [ram, krishna, prasad]
- คลิกซ้ำจน Deque ว่าง
- คลิกอีกครั้ง → Crash (NoSuchElementException)
- ลองแก้โค้ดใช้ pollFirst() จะไม่ Crash