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

วิธีใช้ ArrayBlockingQueue ใน Android

บทนำ

ArrayBlockingQueue เป็น Bounded Blocking Queue ใช้ Array คงที่ ทำงานแบบ FIFO Thread-safe รองรับ Blocking Operations (put/take) ที่รอเมื่อเต็ม/ว่าง เหมาะสำหรับ Producer-Consumer Pattern

ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่

เปิด Android Studio เลือก File → New Project กรอกข้อมูลที่จำเป็น

ขั้นตอนที่ 2: ออกแบบ Layout (res/layout/activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:app="https://schemas.android.com/apk/res-auto"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:gravity="center"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <TextView
        android:id="@+id/actionEvent"
        android:textSize="40sp"
        android:layout_marginTop="30dp"
        android:layout_width="wrap_content"
        android:layout_height="match_parent" />

</LinearLayout>

ขั้นตอนที่ 3: เขียนโค้ด Java (src/MainActivity.java)

package com.example.myapplication;

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.TextView;
import java.util.concurrent.ArrayBlockingQueue;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView actionEvent = findViewById(R.id.actionEvent);

        // สร้าง Queue ความจุ 50
        ArrayBlockingQueue<String> arrayBlockingQueue = new ArrayBlockingQueue<>(50);

        // เพิ่มข้อมูล (Non-blocking)
        arrayBlockingQueue.add("sai");
        arrayBlockingQueue.add("ram");
        arrayBlockingQueue.add("krishna");
        arrayBlockingQueue.add("prasad");

        // แสดง Queue ทั้งหมด
        actionEvent.setText("" + arrayBlockingQueue);
    }
}

การทดสอบแอปพลิเคชัน

รันแอปบนอุปกรณ์จริง

วิธีใช้ ArrayBlockingQueue ใน Android

เมธอดหลักของ ArrayBlockingQueue

ประเภท เมธอด พฤติกรรม
Insert add(e) เพิ่ม, โยน IllegalStateException หากเต็ม
offer(e) เพิ่ม, คืน false หากเต็ม
put(e) เพิ่ม, Block จนกว่าจะมีที่ว่าง
Remove remove() ดึง Head, โยน NoSuchElementException หากว่าง
poll() ดึง Head, คืน null หากว่าง
take() ดึง Head, Block จนกว่าจะมีข้อมูล
Inspect element() ดู Head, โยน Exception หากว่าง
peek() ดู Head, คืน null หากว่าง

Producer-Consumer Example (Concept)

// Producer Thread
new Thread(() -> {
    try {
        for (int i = 0; i < 100; i++) {
            queue.put("Item " + i); // Block หากเต็ม
        }
    } catch (InterruptedException e) { Thread.currentThread().interrupt(); }
}).start();

// Consumer Thread
new Thread(() -> {
    try {
        while (true) {
            String item = queue.take(); // Block หากว่าง
            process(item);
        }
    } catch (InterruptedException e) { Thread.currentThread().interrupt(); }
}).start();

สรุป

ArrayBlockingQueue เหมาะสำหรับ Producer-Consumer ที่ต้องการควบคุม Buffer Size ใช้ put()/take() สำหรับ Blocking, offer()/poll() สำหรับ Non-blocking