Giới thiệu về push() trong ConcurrentLinkedDeque
push(E e) đẩy phần tử vào đầu deque (tương đương addFirst(e)). Trong ConcurrentLinkedDeque, phương thức này:
- Luôn thành công (unbounded)
- Thread-safe (lock-free)
- Không block, không ném exception khi đầy (vì không bao giờ đầy)
Đây là phương thức kế thừa từ interface Deque, cung cấp ngữ nghĩa stack (LIFO).
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.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);
actionEvent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Push thêm vào ĐẦU deque
concurrentLinkedDeque.push("mammahe");
actionEvent.setText("" + concurrentLinkedDeque);
}
});
}
}
Kết quả
Ban đầu: [sai, ram, krishna, prasad]
Sau click (push thêm vào đầu): [mammahe, sai, ram, krishna, prasad]
push() vs addFirst() vs offerFirst()
| Phương thức | Tương đương | Hành vi khi đầy |
|---|---|---|
push(e) |
addFirst(e) |
Ném IllegalStateException (bounded) |
addFirst(e) |
- | Ném IllegalStateException |
offerFirst(e) |
- | Trả về false |
Với ConcurrentLinkedDeque (unbounded): Cả ba đều luôn thành công.
Ứng dụng thực tế - Stack an toàn đa luồng
// Thread-safe stack dùng ConcurrentLinkedDeque
ConcurrentLinkedDeque<Task> taskStack = new ConcurrentLinkedDeque<>();
// Producer threads
taskStack.push(new Task("High priority"));
// Consumer thread
Task task = taskStack.pop(); // Lấy từ đầu (LIFO)
Lưu ý
push()không cho phépnull(ném NPE)- Hiệu năng cao nhờ thuật toán lock-free (CAS)
- Phù hợp cho pattern Stack trong môi trường concurrent