Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Android

Cách sử dụng put() trong PriorityBlockingQueue trên Android

Giới thiệu về put() trong PriorityBlockingQueue

PriorityBlockingQueue là unbounded queue (không giới hạn dung lượng). Do đó, phương thức put(E e) không bao giờ block - nó luôn chèn thành công ngay lập tức.

Điều này khác biệt hoàn toàn với ArrayBlockingQueue hoặc LinkedBlockingQueue bounded, nơi put() sẽ chờ khi queue đầy.

Ví dụ minh họa

Bước 1: Tạo project

Android Studio → File → New Project.

Bước 2: Layout

<?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>

Bước 3: Code (src/MainActivity.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.PriorityBlockingQueue;

public class MainActivity extends AppCompatActivity {
    PriorityBlockingQueue<String> priorityBlockingQueue;

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        priorityBlockingQueue = new PriorityBlockingQueue<>();
        final TextView actionEvent = findViewById(R.id.actionEvent);

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

        actionEvent.setText("" + priorityBlockingQueue);

        actionEvent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Không bao giờ block vì unbounded
                priorityBlockingQueue.put("tutorialspoint.com");
                actionEvent.setText("" + priorityBlockingQueue);
            }
        });
    }
}

Kết quả

Màn hình ban đầu (theo thứ tự alphabet): [prasad, ram, ram, sai, krishna]

Sau click: [prasad, ram, ram, sai, krishna, tutorialspoint.com]

Đặc điểm quan trọng

  • Không block: Luôn trả về ngay lập tức
  • Thread-safe: An toàn cho đa luồng producer
  • Sắp xếp tự động: Theo thứ tự tự nhiên (Comparable) hoặc Comparator
  • Không cho phép null: Sẽ ném NullPointerException

So sánh put() vs add() vs offer()

Phương thức Queue đầy Trả về Ghi chú
add(e) Ném IllegalStateException boolean Chỉ cho bounded queue
offer(e) Trả về false boolean An toàn, không exception
put(e) Block (chờ) void Chỉ block cho bounded queue

Với PriorityBlockingQueue (unbounded): Cả ba phương thức đều luôn thành công và không bao giờ block.

Khi nào dùng PriorityBlockingQueue?

  • Task scheduling theo độ ưu tiên
  • Thread pool với priority
  • Event processing hệ thống