当前位置:网站首页>Usage of Thread, Handler and IntentService
Usage of Thread, Handler and IntentService
2022-08-04 06:05:00 【N_Y_S】
Key points
Thread (thread), Handler (handle), IntentService
Content
When to use threads: When time-consuming tasks need to be performed in the main thread
When the handle is used: Pass the message from the child thread to the main thread.For example: Update UI
Android three ways to open child threads
1. Inherit Thread
Create a child thread method one inherits the Thread classclass MyThread extends Thread{@Overridepublic void run() {//Simulate time-consuming operationtry {sleep(3000); } catch (InterruptedException e) {e.printStackTrace();}tv_text.setText("123456789");An error will be reported here, because the interface of the ui thread cannot be changed after the thread performs time-consuming operationsOnly the original thread that created a view hierarchy can touch its views.}}MyThread myThread=new MyThread();myThread.start();//If you want to set the value of tv_text, you need to use the Handler handle
Second, inherit Runnable
Create a child thread method 2 inherit the Runnable classclass MyRunnable implements Runnable{@Overridepublic void run() {try {There is no sleep method in Runnable, so borrow the method in ThreadThread.sleep(3000);String result="123456";Log.i("Runnable:", result);} catch (InterruptedException e) {e.printStackTrace();}}}MyRunnable myRunnable=new MyRunnable();Thread thread=new Thread(myRunnable);thread.start();Three, use Thread and Runnable at the same time
//Create child thread method 3 Use both Thread and Runnable to create anonymous inner classesnew Thread(new Runnable() {@Overridepublic void run() {try {//Because it is currently in the Runnable interface, we still have to borrow the sleep method of ThreadThread.sleep(3000);String result="Anonymous thread";tv_text.setText(result);} catch (InterruptedException e) {e.printStackTrace();}}}).start();How to define Handler first:
//This method is outdated, generally adding the parameter Looper.getMainLooper()
//private Handler handler=new handler(){};
private Handler handler=new handler(Looper.getMainLooper){
};
//How to pass the message of the child thread to the child thread?
//In the thread's run()
Message msg=new Message();
msg.what=1;
msg.obj="content"
//0x111 is a hexadecimal number
handler.sendEmptyMessage(0x111);
Receive messages in the main thread
private Handler handler=new handler(Looper.getMainLooper){
handleMessage(Message msg){
if(msg.what==0x111){
String result=(Sring) msg.obj;
tv1.setText(result);
}
};
IntentService
IntentService is a subclass of Service
The difference between him and the Service is that the Service will not automatically open or close the thread, but the IntentService will open and close automatically.
边栏推荐
猜你喜欢
随机推荐
【go语言入门笔记】12、指针
TensorFlow2 study notes: 4. The first neural network model, iris classification
智能合约安全——私有数据访问
flink问题整理
Th in thymeleaf: href use notes
ReentrantLock(公平锁、非公平锁)可重入锁原理
CAS与自旋锁、ABA问题
【深度学习21天学习挑战赛】0、搭建学习环境
二月、三月校招面试复盘总结(二)
剑指 Offer 2022/7/2
postgresql 事务隔离级别与锁
记一次flink程序优化
超详细MySQL总结
ISCC2021——web部分
PHP课堂笔记(一)
数据库根据提纲复习
(TensorFlow)——tf.variable_scope和tf.name_scope详解
网络大作业心得笔记
(十)树的基础部分(二)
read and study









