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

วิธีหาความจุคงเหลือ (Remaining Capacity) ของ ArrayBlockingQueue ใน Android

บทนำ

ArrayBlockingQueue มีความจุคงที่ (Bounded) ตัวอย่างนี้สาธิตการใช้ remainingCapacity() เพื่อหาจำนวนช่องว่างที่ยังเพิ่มข้อมูลได้

ขั้นตอนที่ 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.annotation.SuppressLint;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import java.util.concurrent.ArrayBlockingQueue;

public class MainActivity extends AppCompatActivity {

    ArrayBlockingQueue<String> arrayBlockingQueue;

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

        // สร้าง Queue ความจุ 50
        arrayBlockingQueue = new ArrayBlockingQueue<>(50);
        final TextView actionEvent = findViewById(R.id.actionEvent);

        // เพิ่มข้อมูลตัวอย่าง 4 รายการ
        arrayBlockingQueue.add("sai");
        arrayBlockingQueue.add("ram");
        arrayBlockingQueue.add("krishna");
        arrayBlockingQueue.add("prasad");

        actionEvent.setText("" + arrayBlockingQueue);

        // คลิกเพื่อดู Remaining Capacity
        actionEvent.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("SetTextI18n")
            @Override
            public void onClick(View v) {
                int remaining = arrayBlockingQueue.remainingCapacity();
                actionEvent.setText("Remaining: " + remaining);
            }
        });
    }
}

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

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

วิธีหาความจุคงเหลือ (Remaining Capacity) ของ ArrayBlockingQueue ใน Android

คลิก TextView จะแสดง Remaining Capacity = 46 (50 - 4)

วิธีหาความจุคงเหลือ (Remaining Capacity) ของ ArrayBlockingQueue ใน Android

ความสัมพันธ์ของ Capacity

capacity() = size() + remainingCapacity()
// 50 = 4 + 46

เมธอด Capacity ทั้งสาม

เมธอด คำอธิบาย ตัวอย่าง
capacity() ความจุสูงสุดของ Queue (คงที่) 50
size() จำนวนข้อมูลปัจจุบัน 4
remainingCapacity() ช่องว่างที่เหลือ 46

การประยุกต์ใช้

  • Producer ตรวจสอบ remainingCapacity() > 0 ก่อน put() หรือ offer()
  • Consumer ตรวจสอบ size() > 0 ก่อน take() หรือ poll()
  • ช่วยป้องกัน Blocking หรือ Rejection ในระบบ Real-time

สรุป

ใช้ remainingCapacity() เพื่อทราบพื้นที่ว่างใน ArrayBlockingQueue ช่วยควบคุม Flow การทำงาน Producer-Consumer ให้มีประสิทธิภาพ