ก่อนจะยกตัวอย่างเราควรรู้ว่าเธรดคืออะไร เธรดเป็นกระบวนการย่อยที่มีน้ำหนักเบา โดยจะดำเนินการในเบื้องหลังโดยไม่ขัดจังหวะ ui ตัวอย่างนี้สาธิตวิธีใช้หลายเธรดใน Android
ขั้นตอนที่ 1 − สร้างโครงการใหม่ใน Android Studio ไปที่ไฟล์ ⇒ โครงการใหม่และกรอกรายละเอียดที่จำเป็นทั้งหมดเพื่อสร้างโครงการใหม่
ขั้นตอนที่ 2 − เพิ่มรหัสต่อไปนี้ใน res/layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://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"> <EditText android:id="@+id/edit_query" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Enter string" /> <Button android:id="@+id/click" android:layout_marginTop="50dp" style="@style/Base.TextAppearance.AppCompat.Widget.Button.Borderless.Colored" android:layout_width="wrap_content" android:background="#c1c1c1" android:textColor="#FFF" android:layout_height="wrap_content" android:text="Button" /> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/text1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
ในโค้ดข้างต้น เราได้แก้ไขข้อความและมุมมองข้อความแล้ว เมื่อผู้ใช้ป้อนข้อความลงใน edittext จะรอจนถึง 5000 มิลลิวินาที และอัปเดตการดูข้อความทั้งสองด้วยชื่อเธรด
ขั้นตอนที่ 3 − เพิ่มรหัสต่อไปนี้ใน src/MainActivity.java
package com.example.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText edit_query;
TextView textView;
TextView text1;
boolean twice = false;
Thread t = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit_query = findViewById(R.id.edit_query);
textView = findViewById(R.id.text);
text1 = findViewById(R.id.text1);
findViewById(R.id.click).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
runthread();
runthread1();
}
});
}
private void runthread1() {
runOnUiThread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
text1.setText("tutorialspoint.com");
}
});
}
private void runthread() {
twice = true;
if (twice) {
final String s1 = edit_query.getText().toString();
t = new Thread(new Runnable() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
textView.setText(t.getName());
twice = false;
}
});
}
});
t.start();
t.setName(s1);
t.setPriority(Thread.MAX_PRIORITY);
}
}
} มาลองเรียกใช้แอปพลิเคชันของคุณกัน ฉันคิดว่าคุณได้เชื่อมต่ออุปกรณ์มือถือ Android จริงกับคอมพิวเตอร์ของคุณ ในการรันแอพจาก android studio ให้เปิดไฟล์กิจกรรมของโปรเจ็กต์แล้วคลิกไอคอน Run จากแถบเครื่องมือ เลือกอุปกรณ์มือถือของคุณเป็นตัวเลือก แล้วตรวจสอบอุปกรณ์มือถือของคุณซึ่งจะแสดงหน้าจอเริ่มต้นของคุณ -

ในผลลัพธ์ข้างต้น ป้อนข้อความในข้อความแก้ไขแล้วคลิกปุ่ม มันจะผนวกข้อมูลเข้ากับเธรดและนำข้อมูลจากเธรดตามวิธี get name และผนวกเข้ากับ textview ในเธรดที่ 1 เธรดที่สองทำงานในพื้นหลังและอัปเดต textview เป็น tuorialspoint.com