Giới thiệu về ConcurrentLinkedQueue
ConcurrentLinkedQueue là queue không giới hạn, thread-safe, dựa trên linked nodes, FIFO, không cho phép null. Phương thức toArray() chuyển nội dung queue thành mảng (array) tại thời điểm gọi (snapshot).
Bước 1: Tạo dự án mới
Mở Android Studio, chọn File → New Project, điền thông tin cần thiết.
Bước 2: Thiết kế giao diện (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>
Bước 3: Viết logic (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("Queue: " + concurrentLinkedQueue.toString());
actionEvent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Chuyển queue sang mảng Object[]
Object[] array = concurrentLinkedQueue.toArray();
// Hoặc chuyển sang mảng String[] (type-safe)
// String[] stringArray = concurrentLinkedQueue.toArray(new String[0]);
StringBuilder sb = new StringBuilder("Array: [");
for (int i = 0; i < array.length; i++) {
sb.append(array[i]);
if (i < array.length - 1) sb.append(", ");
}
sb.append("]");
actionEvent.setText(sb.toString());
}
});
}
}
Chạy ứng dụng
Kết nối thiết bị (API 21+) và chạy ứng dụng.

Nhấn vào TextView để chuyển sang mảng:
Các overload của toArray():Đặc điểm:
Phương thức Trả về Ghi chú toArray()Object[]Cần cast khi dùng toArray(T[] a)T[]Type-safe, khuyên dùng: toArray(new String[0])
- Tạo snapshot - mảng không thay đổi khi queue thay đổi sau đó
- Thread-safe: có thể gọi trong khi luồng khác modify queue
- Thứ tự mảng = thứ tự duyệt queue (FIFO: head đến tail)
- Hiệu năng: O(n) thời gian và không gian