Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Android

วิธีใช้ remove() ใน ConcurrentLinkedQueue ของ Android

ภาพรวมของ ConcurrentLinkedQueue

ConcurrentLinkedQueue เป็นคิวแบบไม่มีขอบเขต (unbounded) บนโหนดเชื่อมโยง รองรับหลายเธรด (thread-safe) เรียงลำดับ FIFO เพิ่มที่ท้าย ไม่อนุญาตค่า null

ตัวอย่างการใช้งาน remove()

ตัวอย่างนี้สาธิตเมธอด remove(Object o) ซึ่งค้นหาและลบสมาชิก ตัวแรกที่ตรงกัน คืน true หากลบสำเร็จ

ขั้นตอนที่ 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.ConcurrentLinkedQueue;

public class MainActivity extends AppCompatActivity {  // แก้: MainActivityextends
    ConcurrentLinkedQueue<String> concurrentLinkedQueue;

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        concurrentLinkedQueue = new ConcurrentLinkedQueue<>();
        final TextView actionEvent = findViewById(R.id.actionEvent);  // แก้: TextViewactionEvent
        
        concurrentLinkedQueue.add("sai");
        concurrentLinkedQueue.add("ram");
        concurrentLinkedQueue.add("krishna");
        concurrentLinkedQueue.add("prasad");
        concurrentLinkedQueue.add("ram");  // ซ้ำ 2 ตัว
        
        actionEvent.setText("" + concurrentLinkedQueue);
        
        actionEvent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // ลบ "prasad" ตัวเดียว
                boolean removed = concurrentLinkedQueue.remove("prasad");
                actionEvent.setText("Removed: " + removed + " | " + concurrentLinkedQueue);
            }
        });
    }
}

🔧 การแก้ไขจากต้นฉบับ

  • MainActivityextends → MainActivity extends
  • TextViewactionEvent → TextView actionEvent
  • เพิ่ม Generic Type <String>
  • แก้ LinearLayoutxmlns → LinearLayout xmlns

พฤติกรรมของ remove() ใน ConcurrentLinkedQueue

  • ค้นหาจากหัวไปท้าย (FIFO order)
  • ลบ ตัวแรกที่ตรงกัน เท่านั้น
  • คืน true หากลบสำเร็จ, false หากไม่พบ
  • Thread-safe - Non-blocking algorithm (CAS)
  • ไม่โยน Exception หากไม่พบ (ต่างจาก removeFirst())

ตัวอย่างข้อมูล

ก่อน: [sai, ram, krishna, prasad, ram]
         ↑           ↑
       index 0    index 3

remove("prasad"): พบที่ index 3 ลบตัวเดียว
หลัง: [sai, ram, krishna, ram]

remove("ram"): พบที่ index 1 ลบตัวแรก
หลัง: [sai, krishna, ram]  // "ram" ตัวที่ 2 เหลือ

remove() vs poll() vs removeFirstOccurrence()

เมธอดคำอธิบายคืนค่า
remove(Object)ค้นหาและลบตัวแรกที่ตรงกันboolean
poll()นำออกที่หัว (FIFO) คืนค่าE หรือ null
removeFirstOccurrence(Object)เหมือน remove() (Deque method)boolean

⚠️ ข้อควรระวัง

  • ConcurrentLinkedQueue ไม่มี removeFirst() หรือ removeLast() (เป็น Queue ไม่ใช่ Deque)
  • ใช้ poll() สำหรับ FIFO removal (ปลอดภัยกว่า)
  • Iterator เป็น Weakly consistent - อาจไม่เห็นการเปลี่ยนแปลงล่าสุด

วิธีทดสอบ

  1. รันแอป ดูคิว: [sai, ram, krishna, prasad, ram]
  2. คลิก TextView → remove("prasad")
  3. ผลลัพธ์: [sai, ram, krishna, ram]
  4. คลิกอีกครั้ง remove("ram") → [sai, krishna, ram]

กรณีใช้งานจริง

  • Message queue - ลบข้อความเฉพาะ (Cancel specific message)
  • Task queue - ยกเลิก Task ที่ตรงเงื่อนไข
  • Event bus - Unsubscribe listener