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

วิธีดึงจำนวน Thread ปัจจุบันใน Android

บทนำ: Thread ใน Android

Thread (เธรด) คือหน่วยประมวลผลย่อย ทำงานพร้อมกัน (Concurrency) Android ใช้ Main Thread (UI Thread) สำหรับ UI และ Background Thread สำหรับงานหนัก

ตัวอย่าง: แสดงจำนวน Thread ที่ Active อยู่

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

Android Studio → New Project → Empty Activity

ขั้นตอนที่ 2: Layout (activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    android:layout_marginTop="100dp"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/threadCountTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="24sp"
        android:textStyle="bold" />
</LinearLayout>

ขั้นตอนที่ 3: MainActivity.java

package com.example.myapplication;

import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = findViewById(R.id.threadCountTextView);
        
        // Thread.activeCount() = จำนวน Thread ที่ Active ใน Thread Group เดียวกัน
        int count = Thread.activeCount();
        textView.setText("Current Active Thread Count: " + count);
        
        // เพิ่มข้อมูล: ชื่อ Thread ปัจจุบัน
        textView.append("\nCurrent Thread: " + Thread.currentThread().getName());
        textView.append("\nThread ID: " + Thread.currentThread().getId());
    }
}

คำอธิบาย Thread.activeCount()

  • คืนค่าประมาณ (Estimate) จำนวน Thread ที่ Active ใน Thread Group เดียวกับ Thread ปัจจุบัน
  • ไม่ได้นับ Thread ทั้งหมดใน Process (มี Thread Group อื่นอาจมี)
  • รวม Main Thread, Binder Thread, Finalizer, Reference Queue, และ Thread ที่สร้างเอง

Thread สำคัญใน Android App ทั่วไป

Thread Nameหน้าที่
mainMain/UI Thread - จัดการ UI, Event Loop
Binder:1, Binder:2, ...IPC Binder Threads - รับส่งข้อมูลระหว่าง Process
FinalizerDaemonรัน finalize() ของ Object ก่อน GC
FinalizerWatchdogDaemonเฝ้าระวัง Finalizer ค้าง
ReferenceQueueDaemonประมวลผล Reference (Weak/Soft/Phantom)
Jit thread poolJIT Compilation (Android 7+)
Profile Saverบันทึก Profiling Data

การดู Thread ทั้งหมดใน Process (Debug)

// ดู Thread ทั้งหมด (ต้องใช้ Debug API)
Thread.getAllStackTraces().keySet().forEach(thread -> {
    Log.d("THREADS", thread.getName() + " - " + thread.getState());
});

ผลลัพธ์ทั่วไป

Current Active Thread Count: 8
Current Thread: main
Thread ID: 1

ค่าปกติประมาณ 6-12 Thread ขึ้นกับเวอร์ชัน Android และสถานะของแอป

การประยุกต์ใช้

  • Monitoring: ตรวจสอบ Thread Leak (จำนวนเพิ่มขึ้นเรื่อยๆ)
  • Debugging: หา Deadlock, ANR
  • Performance: วัด Concurrency Level

เครื่องมือดู Thread แบบละเอียด

  • Android Studio Profiler → Threads
  • adb shell ps -T
  • adb shell cat /proc//task//stat