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

วิธีใช้ take() ใน Android ArrayBlockingQueue

ก่อนเข้าตัวอย่าง ควรทราบว่า ArrayBlockingQueue คืออะไร นี่คือ Queue แบบจำกัดขนาด (Bounded) ที่ทำงานแบบ FIFO (First-In-First-Out) องค์ประกอบตัวแรกจะอยู่ในคิวนานที่สุด และตัวสุดท้ายจะอยู่สั้นที่สุด

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

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

ไปที่ File → New Project และกรอกรายละเอียดที่จำเป็นทั้งหมด

ขั้นตอนที่ 2: เพิ่มโค้ดใน 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>

ใช้ TextView แสดงองค์ประกอบของ ArrayBlockingQueue

ขั้นตอนที่ 3: เพิ่มโค้ดใน src/MainActivity.java

package com.example.myapplication;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import java.util.concurrent.ArrayBlockingQueue;

public class MainActivity extends AppCompatActivity {
    ArrayBlockingQueue<String> arrayBlockingQueue;
    String head;

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

        arrayBlockingQueue = new ArrayBlockingQueue<>(50); // Capacity = 50
        final TextView actionEvent = findViewById(R.id.actionEvent);

        arrayBlockingQueue.add("sai");
        arrayBlockingQueue.add("ram");
        arrayBlockingQueue.add("krishna");
        arrayBlockingQueue.add("prasad");

        actionEvent.setText("" + arrayBlockingQueue);

        actionEvent.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("SetTextI18n")
            @Override
            public void onClick(View v) {
                try {
                    head = arrayBlockingQueue.take(); // ดึงและลบตัวแรก (Block ถ้าว่าง)
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                actionEvent.setText("" + arrayBlockingQueue);
            }
        });
    }
}

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

รันแอปพลิเคชันบนอุปกรณ์ Android จริง ใน Android Studio คลิกไอคอน Run เลือกอุปกรณ์ แล้วตรวจสอบหน้าจอเริ่มต้น

วิธีใช้ take() ใน Android ArrayBlockingQueue

คลิกที่ TextView จะลบองค์ประกอบตัวแรก ("sai") และแสดงที่เหลือ

วิธีใช้ take() ใน Android ArrayBlockingQueue

คำอธิบายเมธอดสำคัญของ ArrayBlockingQueue

เมธอด พฤติกรรม คืนค่า
take() ดึงและลบหัวคิว Block จนกว่าจะมีข้อมูล หัวคิว (E)
poll() ดึงและลบหัวคิว ไม่ Block คืน null ทันทีถ้าว่าง หัวคิว หรือ null
poll(timeout, unit) ดึงและลบหัวคิว Block ตาม Timeout หัวคิว หรือ null
put(e) เพิ่มท้ายคิว Block จนกว่าจะมีที่ว่าง void
offer(e) เพิ่มท้ายคิว ไม่ Block คืน false ทันทีถ้าเต็ม boolean

เมื่อไหร่ควรใช้ take()?

  • Consumer Thread ที่ต้องรอข้อมูลจาก Producer (Producer-Consumer Pattern)
  • ต้องการให้ Block จนกว่าจะมีข้อมูล (ไม่ต้องทำ Busy Waiting)
  • ระบบที่ต้องการ Backpressure (ควบคุมอัตราการผลิต/บริโภค)