Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Android

วิธีใช้ size() ใน Android ConcurrentLinkedDeque

ก่อนจะเข้าสู่ตัวอย่าง เราควรทราบก่อนว่า ConcurrentLinkedDeque คืออะไร นี่คือ Deque แบบไม่มีขีดจำกัด (unbounded) ที่สร้างขึ้นจากโหนดลิงก์ (linked nodes) ซึ่งรองรับการเข้าถึงองค์ประกอบจากหลายเธรด (multiple threads) อย่างปลอดภัย

ตัวอย่างการใช้งาน size() ใน Android ConcurrentLinkedDeque

ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่ใน Android Studio

ไปที่ File → New Project และกรอกรายละเอียดที่จำเป็นทั้งหมดเพื่อสร้างโปรเจกต์ใหม่

ขั้นตอนที่ 2: เพิ่มโค้ดใน 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>

ในโค้ดข้างต้น เราใช้ TextView เพื่อแสดงองค์ประกอบของ ConcurrentLinkedDeque

ขั้นตอนที่ 3: เพิ่มโค้ดใน 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) {
                actionEvent.setText("" + concurrentLinkedDeque.size());
            }
        });
    }
}

การทดสอบแอปพลิเคชัน

ลองรันแอปพลิเคชันโดยเชื่อมต่ออุปกรณ์ Android จริงกับคอมพิวเตอร์ ใน Android Studio ให้เปิดไฟล์ Activity และคลิกไอคอน Run จากแถบเครื่องมือ เลือกอุปกรณ์มือถือของคุณ แล้วตรวจสอบหน้าจอเริ่มต้น

วิธีใช้ size() ใน Android ConcurrentLinkedDeque

ตอนนี้ลองคลิกที่ TextView จะแสดงผลขนาด (size) ของ Deque ดังภาพด้านล่าง

วิธีใช้ size() ใน Android ConcurrentLinkedDeque

คำอธิบายเพิ่มเติม

  • ConcurrentLinkedDeque เหมาะสำหรับสภาพแวดล้อมมัลติเธรด (multi-threaded) ที่ต้องการคิวสองด้าน (deque) ที่ปลอดภัยต่อเธรด
  • เมธอด size() จะคืนค่าจำนวนองค์ประกอบใน Deque ณ ขณะนั้น
  • เนื่องจากเป็นการทำงานแบบคอนคอร์เรนต์ ผลลัพธ์ของ size() อาจเปลี่ยนไปได้ทุกเมื่อหากมีเธรดอื่นเพิ่ม/ลบข้อมูลพร้อมกัน