Giới thiệu về ConcurrentLinkedDeque
ConcurrentLinkedDeque là deque không giới hạn, thread-safe, dựa trên linked nodes. API 21+. Phương thức clear() xóa tất cả phần tử.
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.ConcurrentLinkedDeque;
public class MainActivity extends AppCompatActivity {
ConcurrentLinkedDeque<String> concurrentLinkedDeque;
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
concurrentLinkedDeque = new ConcurrentLinkedDeque<>();
final TextView actionEvent = findViewById(R.id.actionEvent);
concurrentLinkedDeque.add("sai");
concurrentLinkedDeque.add("ram");
concurrentLinkedDeque.add("krishna");
concurrentLinkedDeque.add("prasad");
actionEvent.setText(concurrentLinkedDeque.toString());
actionEvent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
concurrentLinkedDeque.clear();
actionEvent.setText("Sau clear: " + concurrentLinkedDeque.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 để clear:

Đặc điểm clear():
- Xóa tất cả phần tử, deque trở thành rỗng
- Thread-safe: an toàn gọi đồng thời với add/remove
- Khác với ArrayBlockingQueue.clear(): ConcurrentLinkedDeque không giới hạn dung lượng
- Iterator đang duyệt sẽ thấy thay đổi (weakly consistent) - không ném ConcurrentModificationException
- Phương thức tương đương:
removeAllElements()(legacy)