Giới thiệu về ConcurrentLinkedQueue và poll()
ConcurrentLinkedQueue là hàng đợi không giới hạn (unbounded), thread-safe, dựa trên nút liên kết. Các phần tử tuân theo chiến lược FIFO (First In First Out) - phần tử đầu tiên vào sẽ được lấy ra đầu tiên.
poll() lấy và xóa phần tử đầu tiên (head) của queue. Trả về null nếu queue rỗng - không ném exception.
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.ConcurrentLinkedQueue;
public class MainActivity extends AppCompatActivity {
ConcurrentLinkedQueue<String> concurrentLinkedQueue;
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
concurrentLinkedQueue = new ConcurrentLinkedQueue<>();
final TextView actionEvent = findViewById(R.id.actionEvent);
concurrentLinkedQueue.add("sai");
concurrentLinkedQueue.add("ram");
concurrentLinkedQueue.add("krishna");
concurrentLinkedQueue.add("prasad");
concurrentLinkedQueue.add("ram");
actionEvent.setText("" + concurrentLinkedQueue);
actionEvent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// poll() lấy và xóa phần tử ĐẦU (FIFO)
String removed = concurrentLinkedQueue.poll();
actionEvent.setText("Đã lấy: " + removed + "\nCòn: " + concurrentLinkedQueue);
}
});
}
}
Kết quả
Ban đầu: [sai, ram, krishna, prasad, ram]
Click 1: Lấy "sai" → [ram, krishna, prasad, ram]
Click 2: Lấy "ram" → [krishna, prasad, ram]
Click 3: Lấy "krishna" → [prasad, ram]
... cho đến khi rỗng, tiếp tục click trả về null.
poll() vs remove() vs peek()
| Phương thức | Hành động | Queue rỗng | Trả về |
|---|---|---|---|
poll() |
Lấy VÀ xóa head | Trả về null | E hoặc null |
remove() |
Lấy VÀ xóa head | Ném NoSuchElementException | E |
peek() |
Chỉ xem head (KHÔNG xóa) | Trả về null | E hoặc null |
element() |
Chỉ xem head | Ném NoSuchElementException | E |
Ứng dụng thực tế - Task Queue FIFO
ConcurrentLinkedQueue<Runnable> taskQueue = new ConcurrentLinkedQueue<>();
// Producer threads - thêm task
taskQueue.offer(() -> doWork1());
taskQueue.offer(() -> doWork2());
// Consumer thread - xử lý FIFO
new Thread(() -> {
while (true) {
Runnable task = taskQueue.poll();
if (task != null) {
task.run();
} else {
// Queue rỗng, nghỉ một chút
Thread.sleep(100);
}
}
}).start();
Đặc điểm ConcurrentLinkedQueue
- Unbounded: Không giới hạn dung lượng
- Thread-safe: Lock-free (CAS), hiệu năng cao
- FIFO: Thứ tự vào - ra
- Không cho phép null: Ném NPE
- Iterator weakly consistent: Phản ánh trạng thái tại thời điểm tạo