1、耗时操作都需要使用到线程。
2、开启线程的活动结束后线程未被关闭的话依旧在运行
1)线程的常规操作
代码如下:
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
Log.e("MainActivity", "页面1创建");
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, SecondActivity.class));
}
});
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//传建一个计数线程,每个1S 计数
new Thread(new Runnable() {
@Override
public void run() {
int num = 0;
Log.e("MainActivity", "线程开始");
while (true) {
try {
Thread.sleep(1000);
Log.e("MainActivity", "记数开始:" + num);
num++;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// Log.e("MainActivity", "线程结束"); //循环条件为true 死循环时 后面代码会报错
}
}).start();
}
});
}
@Override
protected void onStop() {
super.onStop();
Log.e("MainActivity", "页面1onStop");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.e("MainActivity", "页面1结束");
}
打印log如下:
01-11 14:17:27.588 26204-26204/com.leixiansheng.test E/MainActivity: 页面1创建
01-11 14:17:34.120 26204-26229/com.leixiansheng.test E/MainActivity: 线程开始
01-11 14:17:35.121 26204-26229/com.leixiansheng.test E/MainActivity: 记数开始:0
01-11 14:17:36.122 26204-26229/com.leixiansheng.test E/MainActivity: 记数开始:1
01-11 14:17:37.123 26204-26229/com.leixiansheng.test E/MainActivity: 记数开始:2
01-11 14:17:37.465 26204-26204/com.leixiansheng.test E/MainActivity: 页面2创建
01-11 14:17:37.819 26204-26204/com.leixiansheng.test E/MainActivity: 页面1onStop
01-11 14:17:38.124 26204-26229/com.leixiansheng.test E/MainActivity: 记数开始:3
01-11 14:17:39.124 26204-26229/com.leixiansheng.test E/MainActivity: 记数开始:4
01-11 14:17:40.125 26204-26229/com.leixiansheng.test E/MainActivity: 记数开始:5
01-11 14:17:40.505 26204-26204/com.leixiansheng.test E/MainActivity: 页面2结束
01-11 14:17:41.126 26204-26229/com.leixiansheng.test E/MainActivity: 记数开始:6
01-11 14:17:42.127 26204-26229/com.leixiansheng.test E/MainActivity: 记数开始:7
01-11 14:17:43.079 26204-26204/com.leixiansheng.test E/MainActivity: 页面1onStop
01-11 14:17:43.079 26204-26204/com.leixiansheng.test E/MainActivity: 页面1结束
01-11 14:17:43.128 26204-26229/com.leixiansheng.test E/MainActivity: 记数开始:8
01-11 14:17:44.128 26204-26229/com.leixiansheng.test E/MainActivity: 记数开始:9
从操作可以看出,开启线程后,进入其他页面线程依旧可以运行。并且有个问题,退出程序后线程还在运行,必须手动杀死程序进程,线程才会停止。(这样就会造成线程泄漏)
2)为了保证程序结束时也停止线程,我们可以引入 volatile 线程标识符
代码如下:
private volatile boolean isStop = false; //线程停止标识符
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
Log.e("MainActivity", "页面1创建");
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, SecondActivity.class));
}
});
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//传建一个计数线程,每个1S 计数
new Thread(new Runnable() {
@Override
public void run() {
int num = 0;
Log.e("MainActivity", "线程开始");
while (!isStop) {
try {
Thread.sleep(1000);
Log.e("MainActivity", "记数开始:" + num);
num++;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Log.e("MainActivity", "线程结束"); //循环条件为true 死循环时 后面代码会报错
}
}).start();
}
});
}
@Override
protected void onStop() {
super.onStop();
Log.e("MainActivity", "页面1onStop");
}
@Override
protected void onDestroy() {
super.onDestroy();
isStop = true;
Log.e("MainActivity", "页面1结束");
}
打印log如下:
01-11 14:41:10.755 27113-27113/com.leixiansheng.test E/MainActivity: 页面1创建
01-11 14:41:22.010 27113-27149/com.leixiansheng.test E/MainActivity: 线程开始
01-11 14:41:23.012 27113-27149/com.leixiansheng.test E/MainActivity: 记数开始:0
01-11 14:41:24.012 27113-27149/com.leixiansheng.test E/MainActivity: 记数开始:1
01-11 14:41:24.816 27113-27113/com.leixiansheng.test E/MainActivity: 页面2创建
01-11 14:41:25.013 27113-27149/com.leixiansheng.test E/MainActivity: 记数开始:2
01-11 14:41:25.168 27113-27113/com.leixiansheng.test E/MainActivity: 页面1onStop
01-11 14:41:26.014 27113-27149/com.leixiansheng.test E/MainActivity: 记数开始:3
01-11 14:41:27.014 27113-27149/com.leixiansheng.test E/MainActivity: 记数开始:4
01-11 14:41:28.015 27113-27149/com.leixiansheng.test E/MainActivity: 记数开始:5
01-11 14:41:28.022 27113-27113/com.leixiansheng.test E/MainActivity: 页面2结束
01-11 14:41:29.016 27113-27149/com.leixiansheng.test E/MainActivity: 记数开始:6
01-11 14:41:30.016 27113-27149/com.leixiansheng.test E/MainActivity: 记数开始:7
01-11 14:41:31.017 27113-27149/com.leixiansheng.test E/MainActivity: 记数开始:8
01-11 14:41:31.135 27113-27113/com.leixiansheng.test E/MainActivity: 页面1onStop
01-11 14:41:31.136 27113-27113/com.leixiansheng.test E/MainActivity: 页面1结束
01-11 14:41:32.018 27113-27149/com.leixiansheng.test E/MainActivity: 记数开始:9
01-11 14:41:32.018 27113-27149/com.leixiansheng.test E/MainActivity: 线程结束
从日志可以看出。开始线程后,进入其他页面线程依旧在运行,程序退出后线程也随程序结束。