当前位置:网站首页>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.
边栏推荐
猜你喜欢
随机推荐
TensorFlow2学习笔记:4、第一个神经网模型,鸢尾花分类
MySQL最左前缀原则【我看懂了hh】
CTFshow—Web入门—信息(9-20)
关系型数据库-MySQL:体系结构
剑指 Offer 2022/7/3
postgres 递归查询
NFT市场可二开开源系统
oracle的number与postgresql的numeric对比
sklearn中的pipeline机制
自动化运维工具Ansible(5)流程控制
SQL的性能分析、优化
npm install dependency error npm ERR! code ENOTFOUNDnpm ERR! syscall getaddrinfonpm ERR! errno ENOTFOUND
(十一)树--堆排序
ISCC2021———MISC部分复现(练武)
数据库根据提纲复习
自动化运维工具Ansible(2)ad-hoc
k9s-终端UI工具
PostgreSQL模式(Schema)
flink sql left join数据倾斜问题解决
BUUCTF——MISC(一)








