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

วิธีใช้ size() ใน LinkedBlockingDeque ของ Android

ภาพรวมของ LinkedBlockingDeque

LinkedBlockingDeque นำ Collection interface และ AbstractQueue มาใช้งาน ให้ขอบเขตตัวเลือกตามโหนดเชื่อมโยง สามารถกำหนดขนาดผ่าน Constructor

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

ตัวอย่างนี้สาธิตเมธอด size() ซึ่งคืนจำนวนสมาชิกใน Deque เป็น O(1)

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

public class MainActivity extends AppCompatActivity {
    LinkedBlockingDeque<String> linkedBlockingDeque;

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        linkedBlockingDeque = new LinkedBlockingDeque<>();
        final TextView actionEvent = findViewById(R.id.actionEvent);
        
        linkedBlockingDeque.add("sai");
        linkedBlockingDeque.add("ram");
        linkedBlockingDeque.add("krishna");
        linkedBlockingDeque.add("prasad");
        linkedBlockingDeque.add("ram");
        
        actionEvent.setText("" + linkedBlockingDeque);
        
        actionEvent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                actionEvent.setText("Size: " + linkedBlockingDeque.size());
            }
        });
    }
}

size() ใน LinkedBlockingDeque

  • คืนจำนวนสมาชิกใน Deque
  • เป็น O(1) - มี Atomic Counter ภายใน
  • Thread-safe - ใช้ Lock (ReentrantLock) แบบ Fine-grained (แยก Lock หัว/ท้าย)
  • นับทั้งหัวและท้าย (Deque นับรวมทุกสมาชิก)

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

เพิ่ม: "sai", "ram", "krishna", "prasad", "ram"
Deque: [sai, ram, krishna, prasad, ram]  // Insertion order
size() = 5

เปรียบเทียบ size() ใน Deque/Queue ต่างๆ

Collectionsize()ComplexityThread-safe
LinkedBlockingDeque✅O(1)✅ Lock-based
ConcurrentLinkedDeque✅O(n)✅ Lock-free
ArrayDeque (Non-thread-safe)✅O(1)❌
LinkedList (Non-thread-safe)✅O(1)❌

🔑 ความแตกต่างสำคัญ: LinkedBlockingDeque vs ConcurrentLinkedDeque

💡 จุดสำคัญ: LinkedBlockingDeque.size() เป็น O(1) เพราะมี Counter
ConcurrentLinkedDeque.size() เป็น O(n) เพราะต้อง Traversal (Lock-free design)

เมธอดที่เกี่ยวข้องกับขนาด

// จำนวนสมาชิก - O(1)
int size()

// เช็คว่าง - O(1)
boolean isEmpty()

// ความจุที่เหลือ (ถ้ากำหนด capacity)
// Unbounded: Integer.MAX_VALUE
// Bounded: capacity - size()
int remainingCapacity()

วิธีทดสอบ

  1. รันแอป ดู Deque: [sai, ram, krishna, prasad, ram]
  2. คลิก TextView → แสดง "Size: 5"
  3. ลองสร้าง Bounded Deque: new LinkedBlockingDeque<>(3)
  4. เพิ่ม 3 ตัว → size()=3, remainingCapacity()=0
  5. ลอง add ตัวที่ 4 → จะ Block (put) หรือ Exception (add) หรือ false (offer)

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

  • Monitoring Deque size สำหรับ Backpressure
  • Bounded buffer - ตรวจสอบว่าเต็มหรือยัง
  • Work stealing queue - ดูขนาดแต่ละ Worker queue